我的应用程序会在许多Android平台(4.1,4.0.4,2.3.6 ......)上报告此问题。 但我无法在手机上重现此问题。 我已经通过Google搜索了这个问题,但堆栈跟踪似乎与我的不一样。
有人知道问题是怎么发生的吗?以及如何预防呢? 或者我该如何重现此错误? 谢谢。
堆栈追踪:
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)
--------------------编辑--------------------
致乔纳森:
我有两个片段。并且只有一个片段会覆盖onPause
回调,代码如下所示。而且我不会在onPause
之后覆盖其他回调。我覆盖的另一个回调是onResume
回调,代码也如下所示。
片段:
@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;
}
}
我还会追踪FragmentActivity
/ FragmentManager
的代码,如果在onAttachedToWindow()
之前调用onPostResume()
,那么问题就会发生。
是否有可能在onAttachedToWindow()
之前调用onPostResume()
?
答案 0 :(得分:10)
此问题是由活动onPaused后提交片段引起的。
一个简单的解决方案是使用FragmentTransaction.commitAllowingStateLoss()
代替FragmentTransaction.commit()