https://blog.csdn.net/freelander_j/article/details/52925745:这篇也不错
可能的原因之一:
Fragment 在显示或者隐藏,移除是出现Can not perform this action after onSaveInstanceState #解决办法:onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后 再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()
可能的原因之二:Activity被系统回收之后,重建时恢复缓存的Fragment的原因 #解决办法之一:在Activity 回收时 onSaveInstanceState 中不缓存Fragment ,在OnCreate 中移除缓存相应Fragment数据,代码如下
private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null && this.clearFragmentsTag()) {
//重建时清除 fragment的状态
savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY);
}
super.onCreate(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (outState != null && this.clearFragmentsTag()) {
//销毁时不保存fragment的状态
outState.remove(BUNDLE_FRAGMENTS_KEY);
}
}
protected boolean clearFragmentsTag() {
return true;
}
————————————————
原文链接:https://blog.csdn.net/swer0808/article/details/92985282
网友评论