8

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();

}
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • try transaction.commitAllowingStateLoss(); – Arth Tilva Dec 29 '15 at 13:29
  • I have tried that, it is not replacing fragment, instead showing blank fragment at that place – Ravi Dec 29 '15 at 13:30
  • can you check [this](http://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-after-onsaveinstanc) , [this](http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-wit),[this](http://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa) – pRaNaY Dec 29 '15 at 13:31
  • @pRaNaY thanks, i have checked all those links but `commitAllowingStateLoss` only stops exception, not replacing fragment – Ravi Dec 29 '15 at 13:34
  • you need use `getSupportFragmentManager()` instead of `getFragmentManager()` while use of **v4** Fragment . Check [here](http://developer.android.com/reference/android/support/v4/app/Fragment.html) – pRaNaY Dec 29 '15 at 13:35
  • @pRaNaY its in another fragment, so i cannot use `getSupportFragmentManager()` – Ravi Dec 29 '15 at 13:36
  • place the 3 lines of code in onStart() and do a check in onStart() to see if onActivityResult() was called ( set some global variable) and execute the 3 lines if the onActivityResult was called. – Kalininskaya Dec 29 '15 at 14:07

3 Answers3

17

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();
    }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 4
    Note for readers: you should not commit your fragment transactions in onResume(). Use onResumeFragments() (for FragmentActivity) or onPostResume() (for Activity) method which will avoid the "java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState" problem. Credits to http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html – vanomart Aug 25 '16 at 07:50
  • nothing is working in my case neither handler or onresume :-( – payal tuteja Jan 19 '17 at 05:33
  • https://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-wit – Exceptional Mar 11 '18 at 07:27
  • 1
    Exactly what I'm looking for! – Maddy Mar 19 '18 at 17:29
4

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

Jarrod
  • 151
  • 6
1

Calling the super.onActivityResult first should fix the issues.

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();
        }
    }
}
DrZaphod
  • 502
  • 1
  • 5
  • 18
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67