我有时会看到以下堆栈跟踪,用于在用户不查看活动时(保存状态后)可能发生的提交:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
查看 Android 源代码,这完全有道理:
private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}
现在,我想知道是否有任何方法(除了将类变量存储在 on(Save/Restore)InstanceState 中)来检查片段是否会以不希望的状态提交,这样我可以存储事务以供以后使用在适当的时间提交。