做项目的时候用到fragment,在fragment快速切换的时候发生了异常:IllegalStateException: Can not perform this action after onSaveInstanceState
这个异常是由于在使用FragmentTransition的 commit方法添加一个Fragment的时候出现的
public abstract int commit ()
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss()
for situations where it may be okay to lose the commit.
Returns
- Returns the identifier of this transaction's back stack entry, if
addToBackStack(String)
had been called. Otherwise, returns a negative number.
public abstract int commitAllowingStateLoss ()
Like commit()
but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.
这个方法与commit()一样,但是允许在activity的状态信息保存后调用。用这个方法是不安全的,有可能activity还没恢复呢就不执行commit方法了。所以这个方法只能用在ui状态转换意外的时候
再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行
了