Android – java.lang.illegalstateexception (this operation cannot be performed after onsaveinstancestate)

This error occurs when I receive the following message in different scenarios:

>Activity corrupted > cannot perform this operation after onsaveinstancestate

I am writing an application in which two activities interact with each other. Activity-a uses intent to start activity-b. this activity-b class has two fragment children. Fragment-a uses the public activity method to start another fragment-b

public void beginTransaction(ID id, Bundle bundle) {

        Fragment fragment = getFragmentItem(id);

        // In case this activity was started with special instructions from an Intent,
        // pass the Intent's extras to the fragment as arguments
        fragment.setArguments(bundle);

        // Add the fragment to the 'fragment_container' FrameLayout
        fragmentManager.beginTransaction()
        .add(R.id.fragment_container, fragment).commitAllowingStateLoss();
    }

Fragment-b calls finish() every time to kill activity-b, so the control returns to activity-a again. The same process is repeated. After 2-3 times, I get:

So to avoid it, I follow https://stackoverflow.com/a/10261438/2624806 , it began to give me

Any suggestion I'm missing here

Got it, I got the solution.. an error occurred in the transaction of adding fragments. I used. Add instead of. Replace (because my request does not maintain the stack) and committed it with commitallowtransaction. I also read it http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html , it seems that what I do may be expensive, But for normal fragment trading, it's good to mention the solution

resolvent:

Due to my low reputation, I can't comment on your question. I think this is a public way to change the fragment

public void beginTransaction(ID id, Bundle bundle)

This method is used every time you add a clip to an activity. Therefore, if you add a clip for the first time, this will work normally, but if it is the second clip, you should use replace instead of adding

    fragmentManager.beginTransaction()
    .replace(R.id.fragment_container, fragment).commitAllowingStateLoss();

I don't think you should face this problem after doing so. Or you can use "replace" for the first and second fragments. I hope this can help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>