Fragment在remove的时候崩溃,报IllegalStateException: Can not perform this action after onSavaInstanc异常
来源:互联网 发布:南光坊天海 知乎 编辑:程序博客网 时间:2024/05/26 08:43
如果在MainActivity中开启一个计时器,这个时候启动OtherActivity,在onFinish结束的时候,执行remove Fragment的时候,这个时候页面会崩溃,报IllegalStateException: Can not perform this action after onSavaInstanc异常。即,移除Fragment使用comit()的时候,未在栈顶会崩溃。
下面是计时器的代码
reserOrderCountdown = new CountDownTimer(second, 1000) {
@Override
public void onTick(long millisUntilFinished) {
int secondTime = (int) (millisUntilFinished / 1000);//将毫秒转换为秒
updateReserOrderTime(secondTime);
}
@Override
public void onFinish() {
removeFragmentReserveOrder();
}
}.start();
@Override
public void onTick(long millisUntilFinished) {
int secondTime = (int) (millisUntilFinished / 1000);//将毫秒转换为秒
updateReserOrderTime(secondTime);
}
@Override
public void onFinish() {
removeFragmentReserveOrder();
}
}.start();
下面试移除Fragment的代码:
Fragment fragment = getFragmentManager().findFragmentById(R.id.map_fragment_content);
getFragmentManager().beginTransaction().remove(fragment).commit();
getFragmentManager().beginTransaction().remove(fragment).commit();
开使用commit()的时候,会崩溃,直接报异常,异常为IllegalStateException: Can not perform this action after onSavaInstanc,
解决方法,将commit()代替为commitAllowingStateLoss():
Fragment fragment = getFragmentManager().findFragmentById(R.id.map_fragment_content);
getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
对比commitAllowingStateLoss()和commit()
public abstract int commit ()
commit不是立即执行,而是当主线程在下一次准备好的时候进行修改,也就是说,这个函数只是把请求放到队列中去。
注意commit操作必须在父容器saving state之前,因为当父容器saving state,意味着父容器可能会被销毁,这个时候这个commit可能会变成无效的。
public abstract int commitAllowingStateLoss ()
很明显这就是我们想要的方法,允许在MainActivity执行onSaveInstanceState后进行Fragment修改。
0 0
- Fragment在remove的时候崩溃,报IllegalStateException: Can not perform this action after onSavaInstanc异常
- fragment 异常IllegalStateException: 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错误: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
- 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
- 解决Fragment偶发异常java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- 解决Fragment偶发异常java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment中使用出现的java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment的checkStateLoss IllegalStateException: Can not perform this action after onSaveInstanceStat
- Fragment的展现与Activity状态丢失 IllegalStateException:Can not perform this action after onSaveInstanceState
- Fragment的Bug: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Can not perform this action after onSaveInstanceState(fragment异常)
- fragment/dialogfragment异常:Can not perform this action after onSaveInstanceState
- SAP快速找到事务码对应程序
- poj1163(数字三角形)的2种解法
- C# 数据库连接之显示数据
- Java集合系列(1)--总体框架
- 微信支付相关流程(总结下最近做微信支付时需要注意的部分)
- Fragment在remove的时候崩溃,报IllegalStateException: Can not perform this action after onSavaInstanc异常
- Ubuntu 14.04挂载NTFS硬盘
- java类
- bzoj1406【数论】
- UVA 12166 Equilibrium Mobile (天平性质 + DFS)
- HTTP基本知识
- JavaScript停止冒泡和阻止浏览器默认行为
- Codeforces 763B-Timofey and rectangles (思维,四色定理)
- atitit 项目注册功能算法attilax总结.docx