0

From an Activity, I call my method to add a new fragment:

protected void addFragment(@NonNull Fragment fragment,
                               @NonNull String fragmentTag,
                               int containerViewId, int enterAnim, int exitAnim,
                               int popEnterAnim, int popExitAnim) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim)
                .add(containerViewId, fragment, fragmentTag);

        transaction = transaction.addToBackStack(null);             
        transaction.commit();
    }

Most of cases all is perfect, but very rarely there is a crash (detected by Crashlytics):

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2044) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2067) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:680) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:634) at com.compagny.ui.BaseActivity.addFragment(BaseActivity.java:1495) at com.compagny.ui.BaseActivity.addFragment(BaseActivity.java:1472) at com.compagny.ui.training.TrainingActivity.setupView(TrainingActivity.java:309)

I don't know why; have you got some suggestions for my investigation guys?

Thank you very much!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
anthony
  • 7,653
  • 8
  • 49
  • 101
  • consider advices in this thread : https://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa?rq=1 – poss Oct 26 '17 at 15:02

1 Answers1

0

The Activity can only transact fragments AFTER onResume and BEFORE onSaveInstanceState.

  • onActivityResult happens before onResume (Not sure what test I did for this one, maybe this statement is invalid)
  • While changing activities they are out of this state (As a callback of a async task)
  • If they come minified to system (As a callback of a async task)
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167