Android-异常处理-Can not perform this action after onSaveInstanceState
来源:互联网 发布:淘宝宝贝描述模板制作 编辑:程序博客网 时间:2024/06/05 18:46
今天在奔溃日志上看到这样的错误信息:
Can not perform this action after onSaveInstanceStatejava.lang.IllegalStateException: Can not perform this action after onSaveInstanceStateat android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1441)at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:581)at android.app.Activity.onBackPressed(Activity.java:2542)
从错误信息我们可以看到,问题所在是Activity#onBackPressed()方法
我们看源码
Activity#onBackPressed()
/** * Called when the activity has detected the user's press of the back * key. The default implementation simply finishes the current activity, * but you can override this to do whatever you want. */ public void onBackPressed() { if (mActionBar != null && mActionBar.collapseActionView()) { return; } if (!mFragments.getFragmentManager().popBackStackImmediate()) { finishAfterTransition(); } }
我们看到onBackPressed()方法执行了两个操作,第一个是获取当前的FragmentManager,并且执行退栈操作,第二个是在退栈完成之后,执行finish方法。
继续查看源码,关键是FragmentManager实现类的popBackStackImmediate方法
@Override public boolean popBackStackImmediate() { checkStateLoss(); return popBackStackImmediate(null, -1, 0); }
我们看到,在执行退栈动作之前,这里还有一步检查操作
private void checkStateLoss() { if (mStateSaved) { throw new IllegalStateException( "Can not perform this action after onSaveInstanceState"); } if (mNoTransactionsBecause != null) { throw new IllegalStateException( "Can not perform this action inside of " + mNoTransactionsBecause); } }
从这里,我们终于找到了崩溃日志上的异常文案:
Can not perform this action after onSaveInstanceState
那么什么时候mStateSaved的值会是true?当Activity被销毁之前,系统会调用onSaveInstanceState来保存当前页面数据,当这个方法被执行之后,mSateSaved会被置为true.
分析
通过上面的源码分析,我们可以知道,出现以上崩溃日志的原因,是因为我们在按下页面返回键的时候,当前Activity以及在执行销毁操作(也就是说我们以前在其他地方调用了finish方法)。
如何解决
方案1 在调用super.onBackPressed的时候,我们需要判断当前Activity是否正在执行销毁操作。
if (!isFinishing()) { super.onBackPressed();}
方案2 通过上面的源码分析,我们也知道了,super.onBackPressed最后也是调用finish()方法,因此我们可以重写onBackPressed,直接调用finish方法。
0 0
- Android-异常处理-Can not perform this action after onSaveInstanceState
- Android 异常 IllegalStateException: Can not perform this action after onSaveInstanceState
- android异常:Can not perform this action after onSaveInstanceState
- 解决异常IllegalStateException: Can not perform this action after onSaveInstanceState
- fragment 异常IllegalStateException: Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState(fragment异常)
- can not perform this action after onsaveinstancestate异常
- Can not perform this action after onSaveInstanceState 异常
- fragment/dialogfragment异常:Can not perform this action after onSaveInstanceState
- IllegalStateException: Can not perform this action after onSaveInstanceState 异常解决
- Can not perform this action after onSaveInstanceState异常源码分析
- 解决Can not perform this action after onSaveInstanceState异常总结
- Fragment异常:Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState
- STM32F103ZET6+ENC28J60+LWIP实现UDP组播
- 内存占用过高导致其他应用block住
- How to custom Spring Batch DelimitedLineTokenizer?
- 【BZOJ】1455 罗马游戏 左偏树
- RecyclerView实现下拉刷新和上拉加载更多
- Android-异常处理-Can not perform this action after onSaveInstanceState
- PHP提交含有CheckBox的表单,比对字符串是否含有相同字母
- 适应不同分辨率的对话框界面
- cpu使用过高导致其余app被block住
- Jquery的parentsUntil()、nextUntil()、sibliings()的区别
- RESTful 异常统一管理
- adb的am start命令启动Activity报Error type 3错误
- 手持(单导联) 心电算法及技术
- docker插件之docker-plugin实现slave的动态扩容与缩容