Skip to content Skip to sidebar Skip to footer

Got Exception: Fragment Already Active

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(b

Solution 1:

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.
*/publicvoidsetArguments(Bundle args){

    if (mIndex >= 0) {
        thrownewIllegalStateException("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 newFragment 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:

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

Solution 3:

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:

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:

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

Post a Comment for "Got Exception: Fragment Already Active"