Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment already active - When trying to setArguments

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?

like image 912
Goofy Avatar asked Nov 15 '13 10:11

Goofy


3 Answers

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.

like image 127
Blehi Avatar answered Oct 07 '22 11:10

Blehi


From the Official Android development Reference:

public void setArguments (Bundle args) 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.

Your fragment is already attached to its activity i suggest you to use your own method, you don't need setArguments!

create your own setUIArguments(Bundle args) inside the fragment class and update the fragment UI inside this method

You will call this method in this way:

@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.setUIArguments(bundle); /* your new method */
} 

in your fragment class

public void setUIArguments(Bundle args) {
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            /* do your UI stuffs */
        }
    }
}
like image 27
Tommaso Resti Avatar answered Oct 07 '22 10:10

Tommaso Resti


You can check if there are already arguments, and if so just add/update them.

private static void initFrag(Fragment frag, Bundle args) {
    if (frag.getArguments() == null) {
        frag.setArguments(args);
    } else {
        //Consider explicitly clearing arguments here
        frag.getArguments().putAll(args);
    }
}

Optionally, you might want to clear away existing arguments if you can't safely assume that pre-existing arguments are still valid.

like image 21
Squimon Avatar answered Oct 07 '22 10:10

Squimon