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

前端 未结 30 2116
迷失自我
迷失自我 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 06:02

    I got this issue.But I think this problem is not related to commit and commitAllowStateLoss.

    The following stack trace and exception message is about commit().

    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    

    But this exception was caused by onBackPressed()

    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(Unknown Source)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(Unknown Source)
    at android.support.v4.app.FragmentActivity.onBackPressed(Unknown Source)
    

    They were all caused by checkStateLoss()

    private void checkStateLoss() {
        if (mStateSaved) {
            throw new IllegalStateException(
                    "Can not perform this action after onSaveInstanceState");
        }
        if (mNoTransactionsBecause != null) {
            throw new IllegalStateException(
                    "Can not perform this action inside of " + mNoTransactionsBecause);
        }
    

    mStateSaved will be true after onSaveInstanceState.

    This problem rarely happens.I have never encountered this problem.I can not reoccurrence the problem.

    I found issue 25517

    It might have occurred in the following circumstances

    1. Back key is called after onSaveInstanceState, but before the new activity is started.

    2. use onStop() in code

    I'm not sure what the root of the problem is. So I used an ugly way.

    @Override
    public void onBackPressed() {
    
        try{
            super.onBackPressed();
        }catch (IllegalStateException e){
            // can output some information here
            finish();
        }
    }
    

提交回复
热议问题