1

I show a DialogFragment inside a button onClick event like below:

public void  onButtonClick() {
    myDialogFragment.show(getSupportFragmentManager(), "dialog");
}

Why does it cause IllegalStateException, may it called after onSaveInstanceState? Here is the log:

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
   at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
   at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
   at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
   at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
   at com.a.b.AFragment.editSex(AFragment.java:226)
   at com.a.b.AFragment.access$200(AFragment.java:46)
   at com.a.b.AFragment$3.onButtonClick(AFragment.java:134)
6

1 Answer 1

1

Inside your show method call this

fragmentTransaction.commitAllowingStateLoss()

after adding fragment to

fragmentTransaction.add();

For reference

Like commit but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

Sign up to request clarification or add additional context in comments.

1 Comment

sometimes it can be a good choice (of course not all the time) but you need to put this on manifest at the activity where is the fragment --> android:configChanges="orientation|screenSize|layoutDirection" (in my case worked)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.