0

Often I have to pass to fragment (or to activity) some interface instance without any internal data. To pass it to fragment (or activity), I should write it to Bundle (or Intent) as Parcelable or Serializable. What do I choose from these two options ?

Example:

public class SomeFragment {

    public static interface Helper {
        View prepareView(SomeFragment fragment, LayoutInflater inflater);
        // etc.
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return extractHelperFromArguments().prepareView(this, inflater);
    }

    public static SomeFragment newInstance(Helper helper) {
        SomeFragment fragment = new SomeFragment();
        // 
        // Bundle args = new Bundle();
        //
        // args.putParcelable(KEY_HELPER, (Parcelable) helper);
        // or
        // args.putSerializable(KEY_HELPER, (Serializable) helper);
        // ?
        //
        // fragment.setArguments(args)
        return fragment;
    }

}

If I choose Parcelable then I have to declare static field CREATOR and empty methods describeContents() and writeToParcel() (empty because the class doesn't have fields).

If I choose Serializable then I don't have to do anything.

Sorry for my English

Leonid Semyonov
  • 387
  • 3
  • 9

1 Answers1

2

Hi here is good link after reading this you will have no more questions regarding Parcelable vs Serializable..

link

enter image description here

khurram
  • 1,362
  • 13
  • 24
  • 2
    if the speed factor was just 2 like on image above i wouldnt bother in implementing Parcelable, in fact i beleive this one is closer to truth: http://www.developerphil.com/parcelable-vs-serializable/ – pskink Oct 22 '14 at 07:54
  • @pskink LOL. Thanks for sharing the link. Parcelable is the way to go despite the simplicity of implementing serializable. – user3144836 Jul 19 '15 at 16:22
  • 1
    This answer's misleading, as the question is specifically about objects that have no fields. The cost of traditional serialization comes from reflection over the class variables, which doesn't cost anything if there aren't any variables to reflect over. – David Liu Sep 23 '15 at 02:32