Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException: Can not perform this action after onSaveInstanceState #516

Closed
OleksandrKucherenko opened this issue Nov 18, 2017 · 13 comments

Comments

@OleksandrKucherenko
Copy link

Issues returns back, reference #205

@OleksandrKucherenko
Copy link
Author

v5.2.1 - crash is still there

java.lang.IllegalStateException: 
  at android.app.FragmentManagerImpl.checkStateLoss (FragmentManager.java:1438)
  at android.app.FragmentManagerImpl.enqueueAction (FragmentManager.java:1456)
  at android.app.BackStackRecord.commitInternal (BackStackRecord.java:707)
  at android.app.BackStackRecord.commit (BackStackRecord.java:671)
  at aue.a (InAppFragment.java:258)
  at aue.onSaveInstanceState (InAppFragment.java:240)
  at android.app.Fragment.performSaveInstanceState (Fragment.java:2514)
  at android.app.FragmentManagerImpl.saveFragmentBasicState (FragmentManager.java:1767)
  at android.app.FragmentManagerImpl.saveAllState (FragmentManager.java:1824)
  at android.app.FragmentController.saveAllState (FragmentController.java:113)
  at android.app.Activity.onSaveInstanceState (Activity.java:1574)
  at gb.onSaveInstanceState (SupportActivity.java:74)
  at fd.onSaveInstanceState (FragmentActivity.java:538)
  at kk.onSaveInstanceState (AppCompatActivity.java:509)
  at bjr.onSaveInstanceState (BaseActivity.java:227)
  at net.easypark.android.mvp.home.MainActivity.onSaveInstanceState (MainActivity.java:229)
  at android.app.Activity.performSaveInstanceState (Activity.java:1503)
  at android.app.Instrumentation.callActivityOnSaveInstanceState (Instrumentation.java:1308)
  at android.app.ActivityThread.callCallActivityOnSaveInstanceState (ActivityThread.java:4943)
  at android.app.ActivityThread.performStopActivityInner (ActivityThread.java:4231)
  at android.app.ActivityThread.handleStopActivity (ActivityThread.java:4290)
  at android.app.ActivityThread.-wrap28 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1665)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6682)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

Problematic code:

    private void cleanUp() {
        if (!mCleanedUp) {
            mHandler.removeCallbacks(mRemover);
            mHandler.removeCallbacks(mDisplayMini);
            UpdateDisplayState.releaseDisplayState(mDisplayStateId);

            final FragmentManager fragmentManager = mParent.getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.remove(this).commit();
        }

        mCleanedUp = true;
    }

Cause of the problem:

  • after call of onSaveInstanceState fragment manager cannot apply any transactions

Possible solution:

  • call cleanUp method from onPause, but do not call from onSaveInstanceState

Take a look into Lifecycle in Depth https://imgur.com/lUtGxVO

@OleksandrKucherenko
Copy link
Author

and make mCleanedUp AtomicBoolean or transient

@solssi
Copy link

solssi commented Nov 23, 2017

Having the same issue: (using compile "com.mixpanel.android:mixpanel-android:5.1.3")

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1438)
       at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1456)
       at android.app.BackStackRecord.commitInternal(BackStackRecord.java:708)
       at android.app.BackStackRecord.commit(BackStackRecord.java:672)
       at com.mixpanel.android.mpmetrics.InAppFragment.cleanUp(InAppFragment.java:247)
       at com.mixpanel.android.mpmetrics.InAppFragment.onSaveInstanceState(InAppFragment.java:229)
       at android.app.Fragment.performSaveInstanceState(Fragment.java:2515)
       at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1767)
       at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1824)
       at android.app.FragmentController.saveAllState(FragmentController.java:113)
       at android.app.Activity.onSaveInstanceState(Activity.java:1578)
       at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:519)
       at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:509)
       at android.app.Activity.performSaveInstanceState(Activity.java:1507)
       at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1308)
       at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:5045)
       at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4313)
       at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4372)
       at android.app.ActivityThread.-wrap28(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1693)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6816)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

@patedit
Copy link
Contributor

patedit commented Nov 28, 2017

Thanks for reporting this guys, will take a look at it soon.

@OleksandrKucherenko
Copy link
Author

any idea when it will be released?! this issue is the most often crash in our app now.

@mrennert91
Copy link

Try fixing like this

private void cleanUp() {
if (!mCleanedUp) {
mHandler.removeCallbacks(mRemover);
mHandler.removeCallbacks(mDisplayMini);
UpdateDisplayState.releaseDisplayState(mDisplayStateId);

        final FragmentManager fragmentManager = mParent.getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.remove(this).commit();
        try {
              transaction.remove(this).commit();
        } catch( IllegalStateException e ) {
              transaction.remove(this).commitAllowingStateLoss();
        }
    }

    mCleanedUp = true;
}

@patedit
Copy link
Contributor

patedit commented Jan 19, 2018

@OleksandrKucherenko based on your comment, is your suspicion that cleanUp() was already called on onPause() and it was called again after onStop()->onSaveInstanceState() but mCleanedUp was still false at the time?

@patedit
Copy link
Contributor

patedit commented Jan 24, 2018

Seems that mCleanedUp is only accessed from the main thread. I went ahead a created a PR that will allow state loss when committing the transaction. The user won't see anything different in that case and hopefully there won't be any more crashes regarding this 👍

Available in the latest release 5.2.4

@OleksandrKucherenko
Copy link
Author

Have to re-open the ticket. http://crashes.to/s/4715c08ed20

code changes raise another exception in the same place.

@OleksandrKucherenko
Copy link
Author

@patedit ^

@patedit
Copy link
Contributor

patedit commented Feb 22, 2018

Hi @OleksandrKucherenko ! I've opened a PR that will address your issue. Would love to hear what you think. Thank you very much for reporting this!

@patedit
Copy link
Contributor

patedit commented Apr 11, 2018

Issue is solved in v5.3.1. Thanks for writing in!

@patedit patedit closed this as completed Apr 11, 2018
@gokhan47
Copy link

What is the reason to save the state if you are ok with commitAllowingStateLoss? This not a fix I believe this is just creating another bug on top of the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants