Reputation: 151
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)
Upvotes: 1
Views: 6746
Reputation: 3045
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.
Upvotes: 1