Displaying a fragment in response to user input raises an IllegalStateException

There are many questions regarding "IllegalStateException: this action cannot be performed after onSaveInstanceState", however I could not find anything that could deal with the situation when this exception occurs when a fragment is displayed in response to user input . (The app is only for Android 4.0+).

In the most illuminating article by Alex Lockwood

Thread: main, Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1265) at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1276) at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541) at android.app.BackStackRecord.commit(BackStackRecord.java:525) at android.app.DialogFragment.show(DialogFragment.java:230) at com.XXX.XXX.XXX.onOptionsItemSelected(SourceFile:XXX) at android.app.Activity.onMenuItemSelected(Activity.java:2507) at android.support.v4.app.g.onMenuItemSelected(SourceFile:372) at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:979) ... 

And this is the method that throws the exception:

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.miAbout) { final DialogFragment df = DialogAbout.newInstance(); //Here we have exception very occasionally: df.show(getFragmentManager(), "about"); return true; } else { return super.onOptionsItemSelected(item); } } 

I know that, perhaps, I could avoid the exception by committing a transaction, allowing a loss of state:

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.miAbout) { final DialogFragment df = DialogAbout.newInstance(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(df, null); ft.commitAllowingStateLoss(); return true; } else { return super.onOptionsItemSelected(item); } } 

However, I still do not understand how it is possible that an exception occurs in response to a user click. Since onSaveInstanceState is called after onPause (in post-Honeycomb), this will mean that the action still processes user clicks after it is suspended (what a surprise!). Or perhaps user clicks are processed with some delay. It would be good to understand what is happening here.

+7
android exception android-fragments
source share

No one has answered this question yet.

See related questions:

5
How to display Dialog dialog box from handler
3
SupportFragment + TabWidget + Samsung Device = "java.lang.IllegalStateException: Can't complete this action after onSaveInstanceState"?
3
Cannot perform this action after displaying onSaveInstanceState
one
fragment.commit () throws an illegalStateException, cannot execute after onSaveInstanceState
0
Snippet does not appear after permission request
0
Fragments: IllegalStateException Cannot complete this action after onSaveInstanceState
0
Can't complete this action after onSaveInstanceState using BottomsheetDialogFragment?
0
add Fragment java.lang.IllegalStateException: this action cannot be completed after onSaveInstanceState
0
IllegalStateException when the user clicks the Close button to close the Dialog box

All Articles