fragment使用时 Can not perform this action after onSaveInstanceState

来源:互联网 发布:申请淘宝商家披露信息 编辑:程序博客网 时间:2024/06/04 18:26

在我们使用fragment的时候有时候会遇到Can not perform this action after onSaveInstanceState错误.总所周知,onSaveInstanceState()函数是在activity被回收以后调用来保存activity的状态的,如果在保存玩状态后再给它添加Fragment就会出错,因此我们有两种方式来处理这样的问题.

  • 1.重写onSaveInstanceState()函数然后将super方法去掉(注意参数的个数).
@Overrideprotected void onSaveInstanceState(Bundle outState) {    //No call for super(). Bug on API Level > 11.}
  • 2.第二种方式就是在使用FragmentTransition不使用 commit方法而是使用另外的一个函数:commitAllowingStateLoss.
transaction.commitAllowingStateLoss();
0 0
原创粉丝点击