Leonid Semyonov
Leonid Semyonov

Reputation: 387

Parcelable vs Serializable - for classes without any fields

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

Upvotes: 0

Views: 251

Answers (1)

khurram
khurram

Reputation: 1362

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

link

enter image description here

Upvotes: 2

Related Questions