0

How can I Add a check to prevent this error

I am getting the error in this code:

private fun clearFragmentsFromContainer() {
    if(supportFragmentManager.backStackEntryCount>0) {
        supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
    }
}

Error on the line:

supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

Log:

 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2080)
        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2106)
        at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:832)
        at com.caring2u.organizer.ui.activities.screen.ActSummaryEvent.clearFragmentsFromContainer(ActSummaryEvent.kt:524)
        at com.caring2u.organizer.ui.activities.screen.ActSummaryEvent.onClickEventTabs(ActSummaryEvent.kt:466)
        at com.caring2u.organizer.ui.activities.screen.ActSummaryEvent.dataEventsList(ActSummaryEvent.kt:162)
        at com.caring2u.organizer.network.retrofit.retrofitTasks.RetroEventsSummary$initiate$1.onResponse(RetroEventsSummary.kt:62)
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 1
    Where exactly you calling `clearFragmentsFromContainer()`? Add code for calling . – ADM Mar 11 '19 at 05:52
  • I have loaded 5 fragments to a container. I am trying to call this method in activity to clear all the fragments in container before starting a new fragment – Devrath Mar 11 '19 at 05:57
  • 1
    I asked Where you call this method not why you calling it . Add code .. See https://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-wit. – ADM Mar 11 '19 at 06:08

4 Answers4

0

You are trying to change the fragment stack after onPause as can be seen from the log.

You can either use FragmentManger.commitAllowingStateLoss or be sure to not call this method after onPause

thepoosh
  • 12,497
  • 15
  • 73
  • 132
0

To remove all fragments in a container please use below code

for (Fragment fragment:getSupportFragmentManager().getFragments()) {
    if (fragment instanceof NavigationDrawerFragment) {
        continue;
    }
    else if (fragment!=null) {
        getSupportFragmentManager().beginTransaction().remove(fragment).commit();
    }
}
Shanto George
  • 994
  • 13
  • 26
0

Probably your fragment transaction is committing after onSavedInstanceState() callback. That means that your activity is stopping and you're adding your fragment in a state that cannot be saved. Actually, during onSavedInstanceState() call Android takes a snapshot of your activity state, this means that if you commit a transaction after the state it's saved the transaction won't be remembered as it was never recorded. From the user point of view that will result in a UI state loss. Instead of using commitAllowingStateLoss you should understand if you're calling your clearFragmentsFromContainer method from an asynchronous method, in that case probably you should simply move your transaction from the async method.

More about "commit state loss":

In order to understand if your activity has already called onSaveInstanceState() method, you might think to place a flag inside onSaveInstanceState callback, resetting the flag in the dual method onRestoreInstanceState, something like:

val saveInstanceStateCalled = false

protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveInstanceStateCalled = true

    }
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    saveInstanceStateCalled = false

}

Then you can check the flag before calling clearFragmentsFromContainer

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
0

I solved this using the code

if (!fragmentManager.isStateSaved()) {
           if(supportFragmentManager.backStackEntryCount>0) {
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
    }
}
Devrath
  • 42,072
  • 54
  • 195
  • 297