I have an app that has a main activity that the user can select an item from. That selection brings up a fragment (TracksActivityFragment) that itself is another list. When an item of that list is selected, a fragment is added that is a DialogFragment. So far so good, but when I rotate the device, the AFragment's onCreate() gets called and then the DailogFragment's onCreate() gets called, then it dies with the IllegalStateException saying that it dies on AFragment's Activity line 20 (setContentView).

Here is a part of that Activity with the line in question:

public class TracksActivity extends AppCompatActivity{private String mArtistName;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_tracks); //DIES HERE

Here is the onCreate of the fragment

@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);if(savedInstanceState !=null){//We've got data saved. Reconstitute itmTrackRowItemList=savedInstanceState.getParcelableArrayList(KEY_ITEMS_LIST);}}

The DialogFragment gets created in the TracksFragment like this:

PlayerFragment fragment=PlayerFragment.newInstance(mTrackRowItemList, i, mArtistBitmapFilename);// The device is smaller, so show the fragment fullscreenFragmentTransaction transaction=fragMan.beginTransaction();// For a little polish, specify a transition animationtransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);// To make it fullscreen, use the 'content' root view as the container// for the fragment, which is always the root view for the activitytransaction.add(android.R.id.content, fragment).addToBackStack(null).commit();

Here is the DialogFragment's onCreate

@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);if (savedInstanceState !=null){//We've got data saved. Reconstitute itif (mPlayer !=null){mPlayer.seekTo(savedInstanceState.getInt(KEY_SONG_POSITION));}}}

Not sure why it goes back to the TracksFragment since it had the DialogFragment active on rotation, but since that is the case, it would seem like I would need to recreate the entireDialogPlayer object, But it seems to keep this around as the call to its onCreate happens.

Anyone know what it is that needs to be done here?

    OK, this was asked before but I discounted the solution...I should not have.

    For some reason, Android wants the Tracks layout XML to use a FrameLayout instead of a fragment.

    So, just replace fragment with FrameLayout in the layout xml file and all is well.

    • it worked for me great!– Tanuj VermaDec 3 '15 at 19:56
    • What is it that FrameLayout replaced? Can you post the original layout to the question?– Al LelopathNov 16 '16 at 18:37
    • I didn't replace fragment with FrameLayout, I just wrapped the fragment in a FrameLayout and that fixed the screen orientation change exception. I don't think I'd ever have guessed that was the cause, the exception message is next to useless in this case.– GlaucusFeb 8 at 4:49

    Your Answer


    By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.