Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing objects in to Fragments

I've been working with lots of Fragments recently and have been using two distinct methods of passing in objects to the Fragments, but the only difference that I can see is that in the approach taken by FragmentOne below, the object you pass in must implement the Serializable interface (and everything associated with that).

Are there any benefits to using one over the other?

public class FragmentOne extends Fragment {     public static final String FRAGMENT_BUNDLE_KEY =          "com.example.FragmentOne.FRAGMENT_BUNDLE_KEY";      public static FragmentOne newInstance(SomeObject someObject) {         FragmentOne f = new FragmentOne();         Bundle args = new Bundle();         args.putSerializable(FRAGMENT_BUNDLE_KEY, someObject);         f.setArguments(args);         return f;     }      public SomeObject getSomeObject() {         return (SomeObject) getArguments().getSerializable(FRAGMENT_BUNDLE_KEY);     } } 

and

public class FragmentTwo extends Fragment {     SomeObject mSomeObject;        public static FragmentTwo newInstance(SomeObject someObject) {         FragmentTwo fragment = new FragmentTwo();         fragment.setSomeObject(someObject);         return fragment;     }      public void setSomeObject(SomeObject someObject) {         mSomeObject = someObject;     } } 
like image 451
Martyn Avatar asked May 31 '12 15:05

Martyn


People also ask

How do you pass between fragments?

To pass a result from a child fragment to a parent, the parent fragment should use getChildFragmentManager() instead of getParentFragmentManager() when calling setFragmentResultListener() . Figure 2 A child fragment can use FragmentManager to send a result to its parent.

How do you pass a class using bundle in fragment?

serialize your model class and put the object in bundle with putSerializable function to pass objects with Fragments. Your activity is hosting those two fragments then why not assign data to activity also ? You would be able to access Activity members from Fragments using getActivity().

How do you pass data between fragments and activities?

So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).

What is a fragment object?

In computing, fragmented objects are truly distributed objects. It is a novel design principle extending the traditional concept of stub based distribution.


2 Answers

There are 3 ways to pass objects to a fragment

They are:

  1. Passing the object through a setter is the fastest way, but state will not be restored automatically.
  2. setArguments with Serializable objects is the slowest way (but okay for small objects, I think) and you have automatic state restoration.
  3. Passing as Parcelable is a fast way (prefer it over 2nd one if you have collection of elements to pass), and you have automatic state restoration.

http://developer.android.com/reference/android/os/Parcelable.html

like image 124
logcat Avatar answered Sep 18 '22 21:09

logcat


for Collection such as List :

I wanted to share my experience.

you need to implement Parcelable

Just use the putParcelableArrayList method.

ArrayList<LClass> localities = new ArrayList<LClass>; ... Bundle bundle = new Bundle(); bundle.putParcelableArrayList(KEY_LClass_LIST, localities); fragmentInstance.setArguments(bundle);  return fragmentInstance; 

And retrieve it using...

localities = savedInstanceState.getParcelableArrayList(KEY_LCLass_LIST); 

So, unless you need the custom ArrayList for some other reason, you can avoid doing any of that extra work and only implement Parcelable for your Locality class.

like image 37
Adnan Abdollah Zaki Avatar answered Sep 18 '22 21:09

Adnan Abdollah Zaki