Fragment already active - When trying to setArguments

Goofy picture Goofy · Nov 15, 2013 · Viewed 19.8k times · Source

I am using the example give in the below link

http://android-er.blogspot.in/2013/04/handle-onlistitemclick-of-listfragment.html

Here i have two classes one extending List Fragment and other extending Fragment. Now i am passing object to detailfragment in this way :

*from ListFragment *

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);

        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_KEY, obj);// passing this object

        detailFragment.setArguments(bundle);
        detailFragment.setUpLayout();// update the UI
} 

Now in the Fragment class i receive it,basic goal is to update the UI of the fragment based on the item selected in the list fragment, thats the reason i am sending the object

Bundle b = getArguments();
b.getSerializable(BUNDLE_KEY);

Now on item selected it says "Fragment already active".

What is the issue here? what am i doing wrong?

Answer

Blehi picture Blehi · Mar 28, 2014

Another solution is to create an empty constructor for your fragment.

public Detailfragment() {
    super();
    // Just to be an empty Bundle. You can use this later with getArguments().set...
    setArguments(new Bundle());
}

and in the onListItemClick method you use that bundle:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);

    // Update the keys.
    detailFragment.getArguments().putSerializable(BUNDLE_KEY, obj);// passing this object

    detailFragment.setUpLayout();// update the UI
} 

Now you can call the getArguments() methond in your setUpLayout() method.