Fragment系列之Can not perform this action after onSaveInstanceState
来源:互联网 发布:mac粉底液专柜价 编辑:程序博客网 时间:2024/05/17 18:03
使用fragment过程当中产生异常的日志信息:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceStateat android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)at android.app.Activity.onBackPressed(Activity.java:2066)at android.app.Activity.onKeyUp(Activity.java:2044)at android.view.KeyEvent.dispatch(KeyEvent.java:2529)at android.app.Activity.dispatchKeyEvent(Activity.java:2274)......
从日志信息我们可以定位到onSaveInstanceState()方法,可以了解到异常原因:在onSaveInstanceState行为之后,app执行某个不能响应的行为而导致异常发生。
stackoverflow上有个解释:
Please check my answer here. Basically I just had to :@Overrideprotected void onSaveInstanceState(Bundle outState) { //No call for super(). Bug on API Level > 11.}Don't make the call to super() on the saveInstanceState method. This was messing things up...This is a known bug in the support package.If you need to save the instance and add something to your outState Bundle you can use the following:@Overrideprotected void onSaveInstanceState(Bundle outState) { outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE"); super.onSaveInstanceState(outState);}In the end the proper solution was (as seen in the comments) to use :transaction.commitAllowingStateLoss();when adding or performing the FragmentTransaction that was causing the Exception.
意思就是:当Activity销毁前调用onSaveInstanceState()方法之后,我们就不能添加或者执行FragmentTransaction ,会导致异常。“This was messing things up…”意思是这样做会丢失状态值可能导致UI状态丢失。(场景:“内存重启”有疑问参见上一篇博文)
解决方案
上文已经贴出来了,建议尽量在 super.onSaveInstanceState(outState)方法之前添加数据,使用commitAllowingStateLoss()替换commit()提交事务,允许状态丢失的提交。(恰巧Activity被强杀时可能导致该次提交无效)
请参考你可能漏掉的知识点: onResumeFragments
这里处理的方法就是保存期它的FragmentTransaction 对象等Activity重新恢复的时候再执行事务,能确保事务不会丢失。
@Override // 如果是在Fragment内, 则复写onResumeFragments()改为onResume()即可 protected void onResumeFragments() { super.onResumeFragments(); if (mTransactionEvent != null) { // 这里执行事务 mTransactionEvent = null; } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //保存事务 给mTransactionEvent赋值 }
虽说方案一所说的Activity被强杀情况下没有执行到commitAllowingStateLoss()方法有可能发生,但是概率很低,我们使用这个方法基本不会有什么问题,方案二虽然麻烦些都是稳一些,用哪个看你自己了。
阅读全文
0 0
- Fragment系列之Can not perform this action after onSaveInstanceState
- Fragment坑之Can not perform this action after onSaveInstanceState
- 解决Fragment IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment 出现Can not perform this action after onSaveInstanceState
- fragment 异常IllegalStateException: Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState(fragment异常)
- Fragment错误:IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment Or DialogFragment Can not perform this action after onSaveInstanceState
- Fragment Can not perform this action after onSaveInstanceState
- Fragment Or DialogFragment Can not perform this action after onSaveInstanceState
- Fragment中出现Can not perform this action after onSaveInstanceState
- 解决fragment错误 Can not perform this action after onSaveInstanceState
- fragment/dialogfragment异常:Can not perform this action after onSaveInstanceState
- fragment使用时 Can not perform this action after onSaveInstanceState
- Fragment异常:Can not perform this action after onSaveInstanceState
- Fragment Can not perform this action after onSaveInstanceState
- 解决Fragment中:Can not perform this action after onSaveInstanceState
- 判断页面鼠标滚动的方向
- CentOS7开机启动管理systemd简介及使用
- Linux 6上使用UDEV绑定共享存储
- 二叉搜索树的基本操作
- tornado和sqlalchemy结合使用
- Fragment系列之Can not perform this action after onSaveInstanceState
- jquery treeview初始化时收缩全部节点并显示指定节点
- NodeJS、NPM安装配置步骤(windows版本)
- DoTween的使用与详解
- tensorflow1.1/autoencoder人脸识别
- 谷歌搜索技巧
- Web服务器使用JSP来创建网页的步骤
- Centos 6.5 安装Oracle 12C RAC
- Python的模块引用和查找路径