ユーザーがアクティビティを表示していないときに発生する可能性のあるコミットについて、次のスタックトレースが表示されることがあります(状態が保存された後)。
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にクラス変数を格納する以外に)があるかどうか疑問に思います。このようにして、後でトランザクションを格納して作成できます。適切なタイミングでコミットします。