Got exception: fragment already active
android, android-emulator, android-fragments, android-intent, android-layout
Solution
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.
Problem
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?