9

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

}
7
  • try transaction.commitAllowingStateLoss(); Commented Dec 29, 2015 at 13:29
  • I have tried that, it is not replacing fragment, instead showing blank fragment at that place Commented Dec 29, 2015 at 13:30
  • can you check this , this,this Commented Dec 29, 2015 at 13:31
  • @pRaNaY thanks, i have checked all those links but commitAllowingStateLoss only stops exception, not replacing fragment Commented Dec 29, 2015 at 13:34
  • you need use getSupportFragmentManager() instead of getFragmentManager() while use of v4 Fragment . Check here Commented Dec 29, 2015 at 13:35

3 Answers 3

18

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 androiddesignpatterns.com/2013/08/…
nothing is working in my case neither handler or onresume :-(
Exactly what I'm looking for!
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.