使用fragment过程当中产生异常的日志信息:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)
at android.app.Activity.onBackPressed(Activity.java:2066)
at android.app.Activity.onKeyUp(Activity.java:2044)
at android.view.KeyEvent.dispatch(KeyEvent.java:2529)
at android.app.Activity.dispatchKeyEvent(Activity.java:2274)
......
从日志信息我们可以定位到onSaveInstanceState()方法,可以了解到异常原因:在onSaveInstanceState行为之后,app执行某个不能响应的行为而导致异常发生。
stackoverflow上有个解释:
Please check my answer here. Basically I just had to :
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
Don't make the call to super() on the saveInstanceState method. This was messing things up...
This is a known bug in the support package.
If you need to save the instance and add something to your outState Bundle you can use the following:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
In the end the proper solution was (as seen in the comments) to use :
transaction.commitAllowingStateLoss();
when adding or performing the FragmentTransaction that was causing the Exception.
意思就是:当Activity销毁前调用onSaveInstanceState()方法之后,我们就不能添加或者执行FragmentTransaction ,会导致异常。“This was messing things up…”意思是这样做会丢失状态值可能导致UI状态丢失。(场景:“内存重启”有疑问参见上一篇博文)
解决方案
上文已经贴出来了,建议尽量在 super.onSaveInstanceState(outState)方法之前添加数据,使用commitAllowingStateLoss()替换commit()提交事务,允许状态丢失的提交。(恰巧Activity被强杀时可能导致该次提交无效)
请参考你可能漏掉的知识点: onResumeFragments
这里处理的方法就是保存期它的FragmentTransaction 对象等Activity重新恢复的时候再执行事务,能确保事务不会丢失。
@Override
// 如果是在Fragment内, 则复写onResumeFragments()改为onResume()即可
protected void onResumeFragments() {
super.onResumeFragments();
if (mTransactionEvent != null) {
// 这里执行事务
mTransactionEvent = null;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//保存事务 给mTransactionEvent赋值
}
虽说方案一所说的Activity被强杀情况下没有执行到commitAllowingStateLoss()方法有可能发生,但是概率很低,我们使用这个方法基本不会有什么问题,方案二虽然麻烦些都是稳一些,用哪个看你自己了。