Got exception: fragment already active

AndroidAndroid LayoutAndroid IntentAndroid EmulatorAndroid Fragments

Android Problem Overview


I have a fragment;

MyFragment myFrag = new MyFragment();

I put bundle data to this fragment:

Bundle bundle = new Bundle(); 
bundle.putString("TEST", "test");
myFrag.setArguments(bundle);

Then, I replace old fragment with this one and put on backstack:

//replace old fragment
fragmentTransaction.replace(R.id.fragment_placeholder, myFrag, "MyTag");
//put on backstack
fragmentTransaction.addToBackStack(null);
//commit & get transaction ID
int transId = fragmentTransaction.commit();

Later, I pop backstack with the above transaction ID(transId):

//pop the transaction from backstack
fragmentManager.popBackStack(transId,FragmentManager.POP_BACK_STACK_INCLUSIVE); 

Later, I set bundle data as argument again to my fragment(myFrag):

//Got Java.lang.IllegalStateException: fragment already active
myFrag.setArguments(bundle);

As you see, my above code got exception Java.lang.IllegalStateException: fragment already active . I don't understand why myFrag is still active though I have popped the transaction of it from backstack., anyhow, since I got the exception I thought I have no choice but de-active the fragment, So, I did:

Fragment activeFragment = fragMgr.findFragmentByTag("MyTag");
fragmentTransaction.remove(activeFragment);

I am not sure if my above code really can de-active the fragment, since I didn't find how to de-active an fragment. :(

After that, when I try to set bundle data to my fragment myFrag again, I still got the same error:

Java.lang.IllegalStateException: fragment already active

Seems even I removed the fragment, it is still active...Why? How to de-active a fragment?

Android Solutions


Solution 1 - Android

Reading the setArguments(Bundle args) source will help you understand:

/**
* Supply the construction arguments for this fragment.  This can only
* be called before the fragment has been attached to its activity; that
* is, you should call it immediately after constructing the fragment.  The
* arguments supplied here will be retained across fragment destroy and
* creation.
*/
public void setArguments(Bundle args) {

    if (mIndex >= 0) {
        throw new IllegalStateException("Fragment already active");
    }
    mArguments = args;
}

You cannot use setArguments(Bundle args) again in your code on the same Fragment. What you want to do I guess is either create a new Fragment and set the arguments again. Or you can use getArguments() and then use the put method of the bundle to change its values.

Solution 2 - Android

Try removing the previous fragment before adding the new one: https://stackoverflow.com/a/6266144/969325

Solution 3 - Android

remove() change fragment status to de-actiive. In your case, you just didn't call commit() after remove(..).

fragmentTransaction.remove(activeFragment);

You would do commit() after remove(), too.

fragmentTransaction.remove(activeFragment).commit();

Solution 4 - Android

Had the same issue. I was adding the fragment to backstack. And the error was because I didn't call popbackstack(). Using popbackstack helped me

Solution 5 - Android

I'm running into the same issue on Xamarin.android. Here's what the documentation says.

> This can only be called before the fragment has been attached to its activity

Solution 6 - Android

Just call public method from fragment

if(userFragment==null){
                    userFragment = new UserFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.EXTRA_CUSTOMER, result);
                    userFragment.setArguments(bundle);
                }else{
                    try {
                        Customer customer = new Customer();
                        customer.parseCustomer(new JSONObject(result));
                        userFragment.updateVeiw(customer);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

Solution 7 - Android

First I start with describing why this happens and then I'll come up with the solution I found working... .

This issue happens when Android is removing the fragment from the stack but is not yet finished with removing. In order to check this, you can use the isRemoving() method of the fragment. If false, i.e. the fragment is not active, you can go on with setting the arguments using setArguments(bundle). Otherwise, you can't set arguments to an already active fragment and can only override it by addressing the same arguments using getArguments().putAll(bundle).

To summarize,

    if (myFrag.isRemoving()) {
        myFrag.getArguments().putAll(bundle);
    } else {
        myFrag.setArguments(bundle);
    }

If you want to avoid this, i.e. removing the fragment at once so there is no active fragment, you might want to use onBackPressed() in onBackStackChangedListener(), which will set the isRemoving() to false.

Solution 8 - Android

  1. Check whether your layout current one or old one for example

     setContentView(R.layout.activity_main);
    
  2. Delete old .gradle file in your project file and rebuild gradle file for project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeem.finView Question on Stackoverflow
Solution 1 - AndroidAssaf GamlielView Answer on Stackoverflow
Solution 2 - AndroidWarpzitView Answer on Stackoverflow
Solution 3 - AndroidDog발자View Answer on Stackoverflow
Solution 4 - AndroidZhangali BidaibekovView Answer on Stackoverflow
Solution 5 - AndroidJonathanView Answer on Stackoverflow
Solution 6 - AndroidKrzysztof DziubaView Answer on Stackoverflow
Solution 7 - AndroidAli NemView Answer on Stackoverflow
Solution 8 - AndroidBalaji BalView Answer on Stackoverflow