上午敲代码时出现这个问题,简单记录一下解决办法,有时间详细描述一下深层原因。 
问题出现在这:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent); 
}

 

 

网上查看了一下,只需修改为如下:

@Override 
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mFragment.isAdded()){
        getSupportFragmentManager().putFragment(outState, "mFragment", mFragment); 
    }
} 

 当我们的应用(即管理多个Fragment的Activity)运行到后台时(即退出当前屏幕),会触发Activity的onPause()方法,而Activity的onPause()会调用它所管理的Fragment的同样的方法,但是当我使用replace时,已经remove掉了原来的Fragment,所以当调用原理的Fragment的onPause()方法就回不存在,就会出现上述xxx is not currently in the FragmentManager的异常推出

以上代码

意思是:在onSaveInstanceState(Bundle outState)方法中保存fragment时,要先确保fragment是否已经加入到fragment manager中。

相关文章:

  • 2021-09-23
  • 2021-12-25
  • 2021-08-31
  • 2021-09-15
  • 2021-12-27
  • 2021-12-05
  • 2022-12-23
猜你喜欢
  • 2021-06-03
  • 2021-11-18
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
相关资源
相似解决方案