Question

Just received two "Crashes & ANRs" on the app store developer console containing the following error:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1354)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1372)

I have logically found the error: I have an async call back which is used to make and HTTP request. If I click on the button which makes the HTTP call and then click back (i.e., close the activity), I receive this issue.

Here is the code which causes the issue:

    ResponseErrorHandler errHdl = new ResponseErrorHandler();
    DialogFragment dialogFragment = errHdl.HandelError(error, responseBody);
    FragmentManager fragMan = getSupportFragmentManager();
    dialogFragment.show(fragMan, TAG + "ErrorDialog");

So if I correctly understand what is happening is that my activity is closing, and then the dialog fragment is trying to show the dialog when the activity is gone.

I do cancel my network request when the activity ends:

@Override
protected void onStop() {
    super.onStop();
    Network.cancelRequests(ConnectingActivity.this);
}
Was it helpful?

Solution

You can check if the activity is being closed/killed before showing the dialog and act accordingly.

E.g.:

if (!parentActivity.isFinishing()) {
    dialogFragment.show(fragMan, TAG + "ErrorDialog");
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top