Home » Android » android – onActivityResult() fragment transaction error

android – onActivityResult() fragment transaction error

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to replace fragment in onActivityResult() but it always gives me error of java.lang.illegalStateException.

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime:     at     android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime:     at .virtualClass.VirtualPurchaseFragment.dealWithSuccessfulPurchase(VirtualPurchaseFragment.java:161)

Here is my onActivityResult code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    Fragment fragment = new VirtualListFragment();
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

}
How to&Answers:

Finally found the solution, fragment can not be swapped or replaced in onActivityResult()

We need to put this code in Handler or in OnResume()

Solution :

private boolean change_fragment=false;

OnActivityResult() Code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    change_fragment=true;
}

OnResume() code

@Override
public void onResume() {
    super.onResume();
    if(change_fragment)
    {
        change_fragment=false;
        Fragment fragment = new VirtualListFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
    }
}

Answer:

The answer to this question is that you should not call commit on a fragment transaction before an activity loads it’s savedInstanceState or after it saves it’s savedInstanceState.

One of the existing answers for this question mentions Activity#onResume() as the place to commit the fragment transaction however Activity#onPostResume() would be a better option since it is called when the instance state of the activity is guaranteed to be restored.

A good explanation of what is happening and why it happens is provided in this blog post: http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html

Answer:

Have you tried this one? hope this one helps

You need to get a fragment manager from your main activity.

Try below snippet:-

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1){
        if(resultCode == Activity.RESULT_OK){
            android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, new DemoFragment(), "fsdf").commit();
        }
    }
}