Can not perform this action after onSaveInstanceState

来源:互联网 发布:千里眼淘宝版下载 编辑:程序博客网 时间:2024/05/17 06:24

项目中出现这个东西,查了源码之后发现

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

大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState

方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就

是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。

源码:

 public abstract int commitAllowingStateLoss();

     * <p class="note">A transaction can only be committed with this method
     * prior to its containing activity saving its state.  If the commit is
     * attempted after that point, an exception will be thrown.  This is
     * because the state after the commit can be lost if the activity needs to
     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
     * situations where it may be okay to lose the commit.</p>

0 0
原创粉丝点击