问题
In runOnUiThread()
of an Activity, I'm trying to show a ProgressDialog.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
ProgressDialogFragment mProgressDialogFragment = new ProgressDialogFragment();
mProgressDialogFragment.show(fragmentTransaction, TAG);
Here ProgressDialogFragment
extends DialogFragment
. I have tested this in many devices and didn't get any crash. But whereas in Asus, I'm constantly getting the below crash whenever I show the dialog. OS version on Asus is 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)
回答1:
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
don't make the call to super()
on the saveInstanceState
method. This was messing things up...
After some more research, this is a know bug in the support package.
If you need to save the instance, and add something to your 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);
}
In the end the proper solution was (as seen in the comments) to use :
transaction.commitAllowingStateLoss();
when adding or performing the FragmentTransaction
that was causing the Exception
.
The above solutions were fixing issues in the early support.v4 libraries from what I can remember. But if you still have issues with this you MUST also read @AlexLockwood 's blog : Fragment Transactions & Activity State Loss
Summary from the blog post (but I strongly recommend you to 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 performing transactions inside asynchronous callback methods
Use commitAllowingStateLoss()
only as a last resort
Updated : For showing the DialogFragment
using stateLoss use the following lines.
DialogFragment loadingDialog = createDialog();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(loadingDialog, "loading");
transaction.commitAllowingStateLoss();
来源:https://stackoverflow.com/questions/30432515/android-showdialog-illegalstateexception-can-not-perform-this-action-after-on