Android ShowDialog: IllegalStateException: Cannot complete this action after onSaveInstanceState
In an runOnUiThread()
activity I am trying to show the ProgressDialog.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
ProgressDialogFragment mProgressDialogFragment = new ProgressDialogFragment();
mProgressDialogFragment.show(fragmentTransaction, TAG);
Here ProgressDialogFragment
continues DialogFragment
. I tested this on many devices and didn't get any crash. But while at Asus, I constantly get below crash when I show dialogue. OS version on Asus - 4.4.2.
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:155)
at com.trimble.android.trimbleone.activity.ProgressDialogHandlerActivity.showProgressDialog(ProgressDialogHandlerActivity.java:72)
at com.trimble.android.trimbleone.activity.UICallbackHandlerActivity$processThread$7.run(UICallbackHandlerActivity.java:222)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(NativeStart.java)
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
do not make a super()
method call saveInstanceState
. It was useless ...
After some more research, this is a bug in the support package.
If you need to keep an instance and add something to yours outState
Bundle
, you can use the following:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
Ultimately the correct solution was (as you can see from the comments):
transaction.commitAllowingStateLoss();
when adding or executing a FragmentTransaction
caller Exception
.
The above solutions were to fix problems in early support.v4 libraries from what I remember. But if you still have trouble with this, you MUST also read @AlexLockwood's blog: Fragment Transactions and Activity State Loss
Summary from blog post (but I highly recommend you read it):
NEVER commit()
transactions after onPause()
on pre-Honeycomb and onStop()
on post-Honeycomb Be careful when committing transactions inside Activity
lifecycle methods. Use onCreate()
, onResumeFragments()
and onPostResume()
Avoid executing transactions inside asynchronous callback methods Use commitAllowingStateLoss()
only as a last resort
Updated . To display DialogFragment
using stateLoss use the following lines.
DialogFragment loadingDialog = createDialog();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(loadingDialog, "loading");
transaction.commitAllowingStateLoss();