Fragment 在显示或者隐藏,移除是出现Can not perform this action after onSaveInstanceState #解决办法:onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后 再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()
try {
var mDismissed: Field = DialogFragment::class.java.getDeclaredField("mDismissed")
mDismissed.isAccessible = true
mDismissed.set(this, false)
var mShownByMe: Field = DialogFragment::class.java.getDeclaredField("mShownByMe")
mShownByMe.isAccessible = true
mShownByMe.set(this, true)
} catch (e: Exception) {
e.printStackTrace()
}
val ft: FragmentTransaction = manager.beginTransaction()
ft.add(this, tag)
ft.commitAllowingStateLoss()
网友评论