有时候我会看到这样一个堆栈跟踪,针对一次提交,当用户没有查看活动(状态已保存)时可能会发生:
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中存储类变量之外,是否有任何方法可以检查片段是否将被提交到不良状态,这样我就可以稍后存储事务并在适当的时间进行提交。
onNewIntent
方法中执行FragmentTransaction
? - Vladimir MironovonNewIntent
添加一个新片段。 - Vladimir Mironov