java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

欢迎各位访问的博客,博客地址

公司app线上这个报错量一直很高,这次修复了下,分析下整个原因:

整个报错栈信息如下:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(SourceFile:1527)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(SourceFile:1545)
    at android.support.v4.app.BackStackRecord.commitInternal(SourceFile:654)
    at android.support.v4.app.BackStackRecord.commit(SourceFile:621)
    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(SourceFile:282)
    at android.view.View.dispatchAttachedToWindow(View.java:13326)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2601)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1334)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1096)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6057)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
    at android.view.Choreographer.doCallbacks(Choreographer.java:606)
    at android.view.Choreographer.doFrame(Choreographer.java:575)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5539)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1、栈顶原因分析:

FragmentManager.checkStateLoss这里抛的异常,翻下源码会发现:如果FragmentTransaction.commit时候activity的状态被保存(即调用了onsaveInstance回调后),就会在这个地方抛这样的异常;

2、相关知识点:

  • 在android HONEYCOMB(Api11)之前:onsaveinstance回调是在onpause之前,在Api11之后调整到了opause之后onstop之前;
  • 从报错信息栈里可以看出我这个项目里用了:FragmentTabHost控件,FragmentTabHost在调用addTab时候会调事务的commit;
  • FragmentTransaction.commit接口是不允许在activity的状态保存之后调用(即调用了onsaveInstance回调后)

3、解决方案:

  • 由于v4支持包中FragmentActivity的onsaveInstance回调存在bug,在回调这个方法时候有可能会有异常,这里重写这个方法不去调用super:
 @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
        //No call for super(). Bug on API Level > 11.
    }
  • 使用FragmentTransaction.commitAllowingStateLoss来提交事务,但由于我这里提交事务是在源码里的,无法复写,故没法处理了;
  • 通过复写FragmentTabHost的onAttachedToWindow方法捕获异常:
 @Override
    protected void onAttachedToWindow() {
        try {
            super.onAttachedToWindow();
        } catch (IllegalStateException e) {
            //由于ft.commit();
        }
    }

后续再看有没有什么好方法。

发布了82 篇原创文章 · 获赞 148 · 访问量 12万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览