40

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;
    }
}
JJD
  • 44,755
  • 49
  • 183
  • 309
Martyn
  • 15,782
  • 24
  • 69
  • 104
  • 1
    possible duplicate of [Proper way to give initial data to fragments](http://stackoverflow.com/questions/10798489/proper-way-to-give-initial-data-to-fragments) – CommonsWare May 31 '12 at 15:35
  • 1
    Also, use `Parcelable` as opposed to `Serlializable`, unless you really need `Serializable` for some non-Android-specific purpose. `Parcelable` is apparently much faster. – CommonsWare May 31 '12 at 15:36
  • Thanks for answering Mark - I feel the other answer you linked to is similar but sufficiently different to what I'm asking here to make this question useful still. – Martyn May 31 '12 at 16:11
  • Then perhaps edit your question to make the differences more apparent. You each want to pass data into a newly-created fragment. In the answer I gave to the question I linked to, I showed the standard pattern for passing data into newly-created fragment. If you are expecting something more, you need to explain what the "more" is. – CommonsWare May 31 '12 at 16:18

2 Answers2

55

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

JJD
  • 44,755
  • 49
  • 183
  • 309
logcat
  • 3,254
  • 1
  • 26
  • 41
  • AFAIK, in 1) you do have automatically restored state if you use setRetainInstance(true) – Jose_GD Jun 04 '13 at 12:43
  • 3
    in 1) setRetainInstance will save instance initialized as is. When we go somewhere else and os kill the app, we can't restore data. So it may be the option in particular cases. – logcat Jun 04 '13 at 15:07
2

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.

Adnan Abdollah Zaki
  • 3,238
  • 5
  • 44
  • 53