getting exception “IllegalStateException: Can not perform this action after onSaveInstanceState”

前端 未结 30 2102
迷失自我
迷失自我 2020-11-22 05:12

I have a Live Android application, and from market i have received following stack trace and i have no idea why its happening as its not happening in application code but it

相关标签:
30条回答
  • 2020-11-22 05:45

    This is fixed in Android 4.2 and also in the support library's source.[*]

    For details of the cause (and work-arounds) refer to the the Google bug report: http://code.google.com/p/android/issues/detail?id=19917

    If you're using the support library then you shouldn't have to worry about this bug (for long)[*]. However, if you're using the API directly (i.e. Not using the support library's FragmentManager) and targeting an API below Android 4.2 then you will need to try one of the work-arounds.

    [*] At the time of writing the Android SDK Manager is still distributing an old version that exhibits this bug.

    Edit I'm going to add some clarification here because I've obviously somehow confused whoever down-voted this answer.

    There are several different (but related) circumstances that can cause this exception to be thrown. My answer above is referring to the specific instance discussed in the question i.e. a bug in Android which has subsequently been fixed. If you're getting this exception for another reason it's because you're adding/removing fragments when you shouldn't be (after fragment states have been saved). If you're in such a situation then perhaps "Nested Fragments - IllegalStateException “Can not perform this action after onSaveInstanceState”" can be of use to you.

    0 讨论(0)
  • 2020-11-22 05:47

    I think Lifecycle state can help to prevent such crash starting from Android support lib v26.1.0 you can have the following check:

    if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)){
      // Do fragment's transaction commit
    }
    

    or you can try:

    Fragment.isStateSaved()
    

    more information here https://developer.android.com/reference/android/support/v4/app/Fragment.html#isStateSaved()

    0 讨论(0)
  • 2020-11-22 05:48

    Read http://chris-alexander.co.uk/on-engineering/dev/android-fragments-within-fragments/

    article. fragment.isResumed() checking helps me in onDestroyView w/o using onSaveInstanceState method.

    0 讨论(0)
  • 2020-11-22 05:48

    My use case: I have used listener in fragment to notify activity that some thing happened. I did new fragment commit on callback method. This works perfectly fine on first time. But on orientation change the activity is recreated with saved instance state. In that case fragment is not created again implies that the fragment have the listener which is old destroyed activity. Any way the call back method will get triggered on action. It goes to destroyed activity which cause the issue. The solution is to reset the listener in fragment with current live activity. This solve the problem.

    0 讨论(0)
  • 2020-11-22 05:50

    I had the same problem, getting IllegalStateException, but replacing all my calls to commit() with commitAllowingStateLoss() did not help.

    The culprit was a call to DialogFragment.show().

    I surround it with

    try {
        dialog.show(transaction, "blah blah");
    }
    catch(IllegalStateException e) {
        return;
    }
    

    and that did it. OK, I don't get to show the dialog, but in this case that was fine.

    It was the only place in my app where I first called FragmentManager.beginTransaction() but never called commit() so I did not find it when I looked for "commit()".

    The funny thing is, the user never leaves the app. Instead the killer was an AdMob interstitial ad showing up.

    0 讨论(0)
  • You may be calling fragmentManager.popBackStackImmediate(); when activity is paused. Activity is not finished but is paused and not on foreground. You need to check whether activity is paused or not before popBackStackImmediate().

    0 讨论(0)
提交回复
热议问题