Android 异常java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
2016-07-01 11:43
676 查看
项目中发现重复跳转一个Fragment出现改异常
这个问题是在使用FragmentTransaction的commit()时出现的异常。网上找了一下资料,顺便看了下源码。发现源码上已有解决方案
上面2个方法得大概意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。if the activity needs to be restored from its state. See {@link #commitAllowingStateLoss()} 。so,把commit换成commitAllowingStateLoss()运行就解决了该bug,记录一下。
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1489) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1507) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
这个问题是在使用FragmentTransaction的commit()时出现的异常。网上找了一下资料,顺便看了下源码。发现源码上已有解决方案
/** * Schedules a commit of this transaction. The commit does * not happen immediately; it will be scheduled as work on the main thread * to be done the next time that thread is ready. * * <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> * * @return Returns the identifier of this transaction's back stack entry, * if {@link #addToBackStack(String)} had been called. Otherwise, returns * a negative number. */ public abstract int commit(); /** * Like {@link #commit} but allows the commit to be executed after an * activity's state is saved. This is dangerous because the commit can * be lost if the activity needs to later be restored from its state, so * this should only be used for cases where it is okay for the UI state * to change unexpectedly on the user. */ public abstract int commitAllowingStateLoss();
上面2个方法得大概意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。if the activity needs to be restored from its state. See {@link #commitAllowingStateLoss()} 。so,把commit换成commitAllowingStateLoss()运行就解决了该bug,记录一下。
相关文章推荐
- android 7.1 输入动态匹配
- Android自定义ListView实现侧滑子菜单
- android 百度地图系列之添加覆盖物和覆盖物的点击事件
- 了解使用Android ConstraintLayout
- Android Studio 好用的插件
- 在Android Studio上使用IdeaVim插件
- android searchview默认展开状态
- Android5.1中Contacts模块拨号加载联系人信息流程
- Android遇见的一些坑
- 利用 LeakCanary 来检查 Android 内存泄漏
- Android AIDL进程间通信介绍
- Android开发中,那些让你觉得相见恨晚的方法、类或接口
- android viewpager 学习使用总结
- Android xml 解析 --详解
- 有关内部类不得不说的
- MAC Android Studio获取SHA1值
- Android组件化和插件化开发
- 关于Android中的Html.fromhtml方法
- 关于Android上GPS获取地理信息坐标的体会
- 关于对Android的videoView和mediaController的吐槽