15

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?

5 Answers 5

18

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.

2
  • This is actually a good idea and I used a mutation of it for the solution. Moreover, this should be set as the correct answer, as the solution revolves around getArguments() and its manipulation - unlike the first answer which is a workaround that won't fit all scenarios.
    – HBN
    Jul 15, 2014 at 16:14
  • 1
    I do not see Fragment having a method called setupLayout() Sep 4, 2014 at 10:29
12

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 */
        }
    }
}
2
  • My class in like this class DetailFragment extends FragmentActivity and inside that i have inner class which extends fragment, now how can i call this method?
    – Goofy
    Nov 15, 2013 at 11:16
  • and also i need context or this in the DetailFragment class, can you please help me with that
    – Goofy
    Nov 15, 2013 at 11:17
12

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.

0

This one global variable:

private FragmentManager fragmentmanager;
private FragmentTransaction fragmenttransaction;

These code put in your "List Fragment" onCreate() Activity :

fragmenttransaction = fragmentmanager.beginTransaction();
fragmenttransaction.replace(detailFragmentID, detailFragment, "test");
fragmenttransaction.addToBackStack(null);
fragmenttransaction.commit();

These is Drawerlistitem click event:

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {

    Bundle bundle = new Bundle();
    fragmenttransaction = fragmentmanager.beginTransaction();

        if(fragmentmanager.findFragmentById("test") != null) {
            fragmenttransaction.remove(fragmentmanager.findFragmentByTag("test"));
        }     

   Detailfragment detailFragment =  (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);

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

       detailFragment.setArguments(bundle);
       fragmenttransaction.replace(detailFragmentID, detailFragment, "test");
       fragmenttransaction.addToBackStack(null);
       fragmenttransaction.commit();    
} 

Now Extending Fragment code as it is:

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

Dialogue fragment's public method

   public void setBundle(final Bundle bundle) {
        final Bundle arguments = getArguments();
        arguments.clear();
        arguments.putAll(bundle);
   }

Show or update dialogue fragment

   public void showMessageDialogue(final String tag, final Bundle bundle) {
        final Fragment fragment = mFragmentManager.findFragmentByTag(tag);
        if (fragment != null && fragment instanceof MessageDialogueFragment) {
            ((MessageDialogueFragment) fragment).setBundle(bundle);
         } else {
            final MessageDialogueFragment messageDialogueFragment = new MessageDialogueFragment();
            messageDialogueFragment.setArguments(bundle);
            messageDialogueFragment.show(mFragmentManager, tag);
         } 
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.