IllegalStateException for FragmentManager

My application continues to report this issue on many Android platforms (4.1, 4.0.4, 2.3.6 ...). But I could not reproduce this problem on my phone. I searched this issue from Google, but the stack trace does not look the same as mine.

Does anyone know how this problem occurs? And how to prevent it? Or how can I reproduce this error? Thanks.

Stack trace:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338) 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.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:278) at android.view.View.dispatchAttachedToWindow(View.java:12064) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2707) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1339) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) at android.view.Choreographer.doCallbacks(Choreographer.java:555) at android.view.Choreographer.doFrame(Choreographer.java:525) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4898) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) at dalvik.system.NativeStart.main(Native Method) 

-------------------- Edit --------------------

To Jonathan:

I have two fragments. And only one fragment overwrites the onPause , and the codes are lower. And I do not overwrite other callbacks after onPause . Another callback I am rewriting is the onResume , the codes are also below.

Fragment:

 @Override public void onPause() { super.onPause(); if (mView instanceof MyView) { MyView my = (MyView) mView; my.onPause(); } } @Override public void onResume() { super.onResume(); if (mView instanceof MyView) { MyView my = (MyView) mView; my.onResume(); } } 

Myview:

 public void onPause() { pause = true; } public void onResume() { pause = false; if (mDialog != null && mDialog.isShowing()) { mDialog.dismiss(); mDialog = null; } } 

I also keep track of the FragmentActivity / FragmentManager codes, it seems that if onAttachedToWindow() is called before onPostResume() , then the problem will occur. Is it possible that onAttachedToWindow() is called before onPostResume() ?

+6
source share
1 answer

This problem is caused by fixing the fragment after the action has been paused.

A simple solution is to use FragmentTransaction.commitAllowingStateLoss() instead of FragmentTransaction.commit()

+9
source

Source: https://habr.com/ru/post/949430/


All Articles