[Solved] Can not perform this action after onSaveInstanceState

IllegalStateException: can not perform this action after onsaveinstancestate exception is one of the possible reasons:

Fragment is displaying or hiding, removing is Can not perform this action after onSaveInstanceState solution: the onSaveInstanceState method is called before the Activity is about to be destroyed, to save the Activity data. If you save the play state and add it to it, it will be wrong. The solution is to replace the commit () method with commitalowingstateloss ()

One of the possible reasons: after the activity is recycled by the system, the cached fragments are recovered during reconstruction. One of the solutions: when the activity is recycled, the fragments are not cached in onsaveinstancestate, and the corresponding fragments are removed from oncreate. The code is as follows’ private static final string bundle ‘_ FRAGMENTS_ KEY = ” android:support :fragments”;

protected void onCreate([@Nullable](https://google.com) Bundle savedInstanceState) {
    if (savedInstanceState != null && this.clearFragmentsTag()) {
        //Clear the state of the fragment when rebuilding

protected void onSaveInstanceState(Bundle outState) {
    if (outState != null && this.clearFragmentsTag()) {
        //Destroy without saving the state of the fragment

protected boolean clearFragmentsTag() {
    return true;


Similar Posts: