我的应用不断在许多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)
- - - - - - - - - - 编辑 - - - - - - - - - -
致乔纳森:
我有两个 fragment 。而且只有一个 fragment 会覆盖
onPause
回调,其代码如下。而且我不会覆盖onPause
之后的其他回调。我覆盖的另一个回调是onResume
回调,代码也如下。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();
}
}
我的 View :
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()
吗? 最佳答案
此问题是由 Activity 被暂停后提交一个 fragment 引起的。
一个简单的解决方案是使用FragmentTransaction.commitAllowingStateLoss()
而不是FragmentTransaction.commit()