【Bug记录 2018-03-30】java.lang.IllegalStateException: Can not perform this action after onSaveInstance

4 篇文章 1 订阅

java.lang.IllegalStateException: Can not perform this action after onSaveInstance

参考 https://blog.csdn.net/chenshufei2/article/details/48747149#t3
http://www.jb51.net/article/113723.htm

原因

首先看下onSaveInstanceState触发时机

Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a useraction (such as pressing the BACK key)

  1. 当用户按下HOME键时。
    这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则
  2. 长按HOME键,选择运行其他的程序时。
  3. 按下电源按键(关闭屏幕显示)时。
  4. 从activity A中启动一个新的activity时。
  5. 屏幕方向切换时,例如从竖屏切换到横屏时。

而DialogFragment#show中默认调用FragmentTransaction#commit,commit提交时不允许状态丢失,否则会抛异常.

解决方法

自己封装Fragment或者DialogFragment,重写show方法和dismiss方法

  public void show(AppCompatActivity activity) {

        FragmentTransaction  ft=activity.getSupportFragmentManager().beginTransaction();
        ft.add(this, this.getClass().getSimpleName());
        ft.commitAllowingStateLoss();//注意这里使用commitAllowingStateLoss()
    }


   public void dismiss() {
        dismissAllowingStateLoss();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值