java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment android android

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment


Here is the answer in another thread:

Actions in onActivityResult and "Error Can not perform this action after onSaveInstanceState"

also here:

Refreshing my fragment not working like I thought it should

This is an example solving this problem:

DialogFragment loadingDialog = createDialog();FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();                    transaction.add(loadingDialog, "loading");                    transaction.commitAllowingStateLoss();  


I got the same issue and I changed the code as bellow

newFragment.show(transactionFragment, "dialog");

to:

transactionFragment.add(android.R.id.content, newFragment).addToBackStack(null).commitAllowingStateLoss();

the completed code works well for me as bellow, hope that help

FragmentTransaction transactionFragment = getActivity().getSupportFragmentManager().beginTransaction();    DialogPageListFragment newFragment = new DialogPageListFragment();    transactionFragment.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);    newFragment.setArguments(extras);    transactionFragment.add(android.R.id.content, newFragment).addToBackStack(null).commitAllowingStateLoss();


Probably, the handler that is responding to the HandleMessage is associated to a destroyed activity.

i.e.: If you rotate the screen, the old destroyed activity will handle the message, then you will call showDialog, and the exception will be thrown:

You are creating a dialog after the old destroyed activity has called his onSaveInstanceState.

Try replacing the callback, with the new created activity, to make sure that you are creating the dialog always in the alive activity.


If you are not rotating, put a flag on onSaveInstance like "saving", and disabling it on onRestoreInstance. In your handleMessage method, if the flag "saving" is on, don't show the dialog, just turn on another flag indicating that the dialog must be created on onResume. Then on onResume method, check if in middle of that process, you should create the dialog, if yes, show it on onResume method.