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

前端 未结 30 2036
迷失自我
迷失自我 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:58

    When i use startactivity in one fragment, i will get this exception;

    When i change to use startactivityforresult, the exception is gone :)

    So the easy way to fix it is use the startActivityForResult api :)

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

    What I found is that if another app is dialog type and allows touches to be sent to background app then almost any background app will crash with this error. I think we need to check every time a transaction is performed if the instance was saved or restored.

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

    In my case, with the same error exception, i put the "onBackPressed()" in a runnable (you can use any of your view):

    myView.post(new Runnable() {
                        @Override
                        public void run() {
                            onBackPressed()
                        }
                    });
    

    I do not understand why, but it works!

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

    After researching a bit the solution to this problem is to do your fragment commits in the onresume.

    Source: https://wenchaojames.wordpress.com/2013/01/12/illegalstateexception-from-onactivityresult/

    0 讨论(0)
  • 2020-11-22 06:01

    Short And working Solution :

    Follow Simple Steps :

    Step 1 : Override onSaveInstanceState state in respective fragment. And remove super method from it.

    @Override
    public void onSaveInstanceState(Bundle outState) {
    };
    

    Step 2 : Use CommitAllowingStateLoss(); instead of commit(); while fragment operations.

    fragmentTransaction.commitAllowingStateLoss();
    
    0 讨论(0)
  • 2020-11-22 06:02

    This is the most stupid bug I have encountered so far. I had a Fragment application working perfectly for API < 11, and Force Closing on API > 11.

    I really couldn't figure out what they changed inside the Activity lifecycle in the call to saveInstance, but I here is how I solved this :

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //No call for super(). Bug on API Level > 11.
    }
    

    I just do not make the call to .super() and everything works great. I hope this will save you some time.

    EDIT: after some more research, this is a known bug in the support package.

    If you need to save the instance, and add something to your outState Bundle you can use the following :

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
        super.onSaveInstanceState(outState);
    }
    

    EDIT2: this may also occur if you are trying to perform a transaction after your Activity is gone in background. To avoid this you should use commitAllowingStateLoss()

    EDIT3: The above solutions were fixing issues in the early support.v4 libraries from what I can remember. But if you still have issues with this you MUST also read @AlexLockwood 's blog : Fragment Transactions & Activity State Loss

    Summary from the blog post (but I strongly recommend you to read it) :

    • NEVER commit() transactions after onPause() on pre-Honeycomb, and onStop() on post-Honeycomb
    • Be careful when committing transactions inside Activity lifecycle methods. Use onCreate(), onResumeFragments() and onPostResume()
    • Avoid performing transactions inside asynchronous callback methods
    • Use commitAllowingStateLoss() only as a last resort
    0 讨论(0)
提交回复
热议问题