IllegalStateException: Can not perform this action after onSaveInstanceState解决办法
来源:互联网 发布:js继承题目 编辑:程序博客网 时间:2024/06/09 23:57
IllegalStateException: Can not perform this action after onSaveInstanceState解决办法
1. 官方描述:
首先我们需要弄清楚onSaveInstanceState方法是在什么时候会被调用,当该Activity即将被销毁前或者按home键退出时调用,来保存Activity数据的,如果在保存玩状态后,桌面通过其他方式启动程序且需要进行Fragment的入栈或弹栈操作就会出错。桌面其他方式如widget、悬浮窗、通知状态栏等方式通过Intent启动程序。此时拦截该Intent可以在下面的方法中进行
@Override protected void onNewIntent(Intent intent) { // 拦截Intent,做相应处理 }2. 问题的原因:
如果在onNewIntent()这里拦截了Intent进行Fragment的添加和弹栈就会报错:
IllegalStateException: Can not perform this action after onSaveInstanceState。
不能在onSaveInstanceState之后立即进行这些操作。
3.解决办法目前找到两种
(1)在onResume执行Fragment的入栈或弹栈操作,即在onNewIntent中保存Intent,在onResume()中进行处理。推荐使用
private Intent onHomeIntent; // home键退出后通过intent启动程序@Overrideprotected void onNewIntent(Intent intent) { // 拦截Intent,保存Intent,在onResume中进行处理 onHomeIntent = intent;}@Overridepublic void onResume() { if (onHomeIntent != null) {// home键退出后通过intent启动程序 // dosomething··· onHomeIntent = null; } super.onResume();}
(2)使用Handler.post()将操作添加到UI消息队列中,这个会在主UI线程执行完成后会执行,即执行完了onResume()之后,这样会导致有段时间的延迟,会造成先回复home键退出之前的页面,然后停留几秒钟执行入栈或弹栈操作。体验效果不是很好。因此推荐使用第一种方法,第一种方法会在onResume()进行操作,执行的时机比较好,不会出现延迟。
new Handler().post(new Runnable() {@Overridepublic void run() { // do something··· }});
0 0
- IllegalStateException: Can not perform this action after onSaveInstanceState解决办法
- IllegalStateException: Can not perform this action after onSaveInstanceState解决办法
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- Android 异常 IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决Fragment IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决异常IllegalStateException: Can not perform this action after onSaveInstanceState
- IllegalStateException: Can not perform this action after onSaveInstanceState
- Geography V.S. Geometry
- 积分制管理系统:添加成员
- 利用 matlab 读取SAR 元数据
- android 图片解码显示流程
- 黑马程序员--.NET笔记--枚举、数组、排序、方法、变量的作用域、重载
- IllegalStateException: Can not perform this action after onSaveInstanceState解决办法
- web.xml
- 48_leetcode_Next Permutation
- xml的开发及文件表
- boost::format sprintf QString 字符串处理
- TextFx-Ease Function(缓动函数)
- MYSQL字段,表名大小写的问题 CASE IN-SENSITIVE MYSQL ON LINUX
- 账务账号的修改
- daemon 的命名守则