在使用Fragment的过程中,常常会遇到在Activity的onSaveInstanceState方法调用之后,操作commit或者popBackStack而导致的crash.
因为在onSaveInstanceState方法之后的操作状态可能会丢失,因此Android framework默认会抛出一个异常.
对于commit方法来说,单纯避免这个异常很简单,使用commitAllowingStateLoss方法即可.但是popBackStack以及
popBackStackImmediate也都会检查state(checkStateLoss),特别需要注意的是Activity的
onBackPressed方法

如果onBackPressed在onSavedInstanceState之后调用,那么就会crash.

onBackPressed的调用时机:

* targetSdkVersion <= 5,在onKeyDown中调用
* targetSdkVersion > 5,在onKeyUp中调用
onSavedInstanceState的调用时机(如果调用的话):

* 一定在onStop之前
* 可能在onPause之前,也可能在onPause与onStop之间
需要注意的是: onSavedInstanceState方法不一定会调用,只有在Activity因为某些原因而被Framework销毁,并且之后还需要重新创建的情况,才需要调用(例如:旋屏,或者内存不足而回收返回栈中的某些Activity)

举例:
* Activity A在前台时,屏幕逐渐变暗直至锁屏,那么A的onSavedInstanceState会被调用
* Activity A start Activity B,Activity A的onSavedInstanceState会被调用
* Activity A因为返回键或者finish调用而返回到上一个界面,那么A的onSavedInstanceState不会被调用
因此,当onBackPressed在onSavedInstanceState方法之后调用,就一定会crash.解决方法主要有两种:

重写Activity的onSavedInstanceState()方法,并且注释掉super调用.
这种方法能避免crash,但是它会导致整个Activity的状态丢失.以DialogFragment为例,正常情况下,显示的
DialogFragment在旋屏Activity重新创建之后,不需要我们处理,Dialog会自动显示出来(参见
DialogFragment.onStart()),但是注释掉Activity的onSavedInstanceState()方法之
后,Fragment状态丢失,Activity重新创建之后,Dialog也就不会再显示出来了.

更好且通用的做法:在调用commit,popBackStack以及onBackPressed方法之前,判断
onSavedInstanceState()方法是否已经执行,并且onResume方法还没有执行,如果不是,那么直接操作,否则加入到
pending队列,等待onResumeFragments或者onPostResume之后再执行.

注意:不要在onResume中操作,因为这时候FragmentManager中的mStateSaved依然可能是true.(如果执行顺序是
onSavedInstanceState()->onPause()->onResume() 或者
onPause()->onSavedInstanceState()->onResume());

public void endPaintingPager(int index) {
if (mFirstLevel == PAINTING_PAGER) {
mFirstLevel = PAINTER_START;
if (!mIsStateSaved) {
getSupportFragmentManager().popBackStack();
} else {
mPopBackStackRunnable = new Runnable() {
@Override
public void run() {
getSupportFragmentManager().popBackStack();
}
};
}
}
}
@Override
protected void onPostResume() {
super.onPostResume();
if (mPopBackStackRunnable != null) {
mPopBackStackRunnable.run();
}
}

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState的更多相关文章

  1. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决

    (1)我用的是fragment,在onStop但是没有onDestroy的情况下切换(replace)fragment时报 java.lang.IllegalStateException: Can n ...

  2. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState解决?

    做项目到最后整合的时候测试的时候发现  切换tab更换fragment的时候抛出了这个异常,根据异常信息Can not perform this action after onSaveInstance ...

  3. 解决IllegalStateException: Can not perform this action after onSaveInstanceState

    今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState ...

  4. 【转】 解决IllegalStateException: Can not perform this action after onSaveInstanceState

    今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState ...

  5. 解决IllegalStateException: Can not perform this action after onSaveInstanceState:

    今天做项目中的支付宝功能,是在fragment中做的,在支付成功后,想切换到支付成功的页面. 结果就报错了IllegalStateException: Can not perform this act ...

  6. IllegalStateException: Can not perform this action after onSaveInstanceState

    http://www.cnblogs.com/zgz345/archive/2013/03/04/2942553.html 今天使用Fragment的时候,出现了这个错误 IllegalStateEx ...

  7. Android 错误:IllegalStateException: Can not perform this action after onSaveInstanceState

    今天做Fragment切换.状态保存功能的时候,出现了这个错误: E/AndroidRuntime(12747): Caused by: java.lang.IllegalStateException ...

  8. [Android Pro] java.lang.IllegalStateException: Fragment(XXFragment) not attached to Activity异常

    转载:http://blog.csdn.net/winson_jason/article/details/20357435 下边两个问题,是在开发中碰到的一些关于Fragment的偶发性的问题,今天时 ...

  9. Can not perform this action after onSaveInstanceState

    java.lang.RuntimeException: Unable to resume activity {com.tongyan.nanjing.subway/com.tongyan.struct ...

随机推荐

  1. Drainage Ditches(Dinic最大流)

    http://poj.org/problem?id=1273 用Dinic求最大流的模板题,注意会有重边. 邻接矩阵建图 #include<stdio.h> #include<str ...

  2. 向SharePoint页面添加后台代码

    转:http://www.cnblogs.com/chenzehe/archive/2009/12/25/1631863.html 在本文中,我将跟大家一起讨论,为MOSS的页面添加服务器端代码的另一 ...

  3. 开源(免费)三维 GIS(地形,游戏) 续1

    转自:http://www.cnblogs.com/xiexiaokui/archive/2009/04/02/1428525.html 转自 三维数字地球发布平台探索--几款开源软件介绍 http: ...

  4. 卸载系统自带的JDK的脚本并再次安装

    卸载系统自带的JDK的脚本并安装1.6.0.32版本的jdk #!/bin/bash homefile=/usr/local/java cd $homefile homelist=`sudo rpm ...

  5. WCF性能优势体现 【转】

    WCF性能优势决定了其受欢迎程度,这些优势主要都体现在:统一性:互操作性:安全与可信赖:兼容性等方面. WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架. ...

  6. Python手动构造Cookie模拟登录后获取网站页面内容

    最近有个好友让我帮忙爬取个小说,这个小说是前三十章直接可读,后面章节需要充值VIP可见.所以就需要利用VIP账户登录后,构造Cookie,再用Python的获取每章节的url,得到内容后再使用 PyQ ...

  7. sql date 的精度问题

    new  java.sql.Date(...) 是经过yyyy-MM-dd 格式化后的时间格式. 如果需要:HH:mm:ss .则要用 new java.sql.Timestamp(.....);

  8. Yii学习系列:Yii视频讲义——前篇(转)

    1.yii的网址 http://www.yiiframework.com/ yii官方网址 http://www.yiichina.com/ yii中文社区 2.bootstrap的网址 http:/ ...

  9. 【转】【opencv】仿射变换

    仿射变换 目标 在这个教程中你将学习到如何: 使用OpenCV函数 warpAffine 来实现一些简单的重映射. 使用OpenCV函数 getRotationMatrix2D 来获得一个  旋转矩阵 ...

  10. Linux 上不可修改的文件和目录

         有时候我们需要让一个我们自己的目录中的内容不能变动,也就是不允许其他人随便删改我们的目录和目录中的文件.这里,首先,我们需要知道两个概念,文件的粘滞位和属性.       文件如果设置了粘滞 ...