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

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

    This happens whenever you are trying to load a fragment but the activity has changed its state to onPause().This happens for example when you try to fetch data and load it to the activity but by the time the user has clicked some button and has moved to next activity.

    You can solve this in two ways

    You can use transaction.commitAllowingStateLoss() instead of transaction.commit() to load fragment but you may end up losing commit operation that is done.

    or

    Make sure that activity is in resume and not going to pause state when loading a fragment. Create a boolean and check if activity is not going to onPause() state.

    @Override
    public void onResume() {
        super.onResume();
        mIsResumed = true;
    }
    
    @Override
    public void onPause() {
        mIsResumed = false;
        super.onPause();
    }
    

    then while loading fragment check if activity is present and load only when activity is foreground.

    if(mIsResumed){
     //load the fragment
    }
    

提交回复
热议问题