异常日志
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1533)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:603)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:179)
at com.shirley.tealeaf.base.BaseActivity.onBackPressed(BaseActivity.java:129)
at com.shirley.tealeaf.login.LoginActivity.isCompletionSuccess(LoginActivity.java:159)
at com.shirley.tealeaf.presenter.LoginPresenter$3.call(LoginPresenter.java:86)
at com.shirley.tealeaf.presenter.LoginPresenter$3.call(LoginPresenter.java:83)
at rx.internal.util.ActionSubscriber.onNext(ActionSubscriber.java:39)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
at rx.internal.operators.OnSubscribeOnAssembly$OnAssemblySubscriber.onNext(OnSubscribeOnAssembly.java:124)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:224)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
异常说明
Fragment
在API < 11之前完美运行,在 API > 11之后则会出现此bug
解决方案
重写onSaveInstanceState
方法
不要在onSaveInstanceState
方法中调用super.
方法
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
保存实例
这是一个support包中比较常见的bug,如果你想保存一些实例,你可以:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
出现场景
如果你的activity在后台被系统杀死后,你执行了fragment
的提交事务操作,则会出现此bug。为了避免发生该bug,你应该使用commitAllowingStateLoss()
参考Fragment Transactions & Activity State Loss
注意
- 在
Honeycomb
之前,不要在onPause()
方法中commit()
事务;在Honeycomb
之后,不要在onStop()
方法中commit()
事务 - 在Activity生命周期方法中提交事务时要小心。尽量在
onCreate()
,onResumeFragments()
和onPostResume()
方法中提交 - 使用
commitAllowingStateLoss()
作最后的手段