Android – Implement Parcelable to pass an object via Bundle

If you want to pass an object via Bundle you have to serialize it or to implement the Parcelable interface (it is an Android Implementation for Java serializable). It is recommended the second option because it is faster than the first one.

Steps to implement Parcable:

  1. … implements Parcelable
How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ

2. Implement the two methods, describeContents() and writeToParcel()

How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ

If you write an other parcelable you should do it this way:

dest.writeParcelable(parcelableName, 0);

3. All the classes implementing the Parcelable interface must also have a static field called CREATOR that implements the Parcelable.Creator interface.

How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ

4. Now implement the methods createFromParcel() and newArray():

How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ
How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ

You should have a private constructor in your class that read the values from the Parcel in the same order your wrote them in the Parcel.

How to implement Parcelable interface, <span>Android – Implement Parcelable to pass an object via Bundle</span>, KandZ

If you read an other parcelable you should do it this way:

parcelableName = source.readParcelable(ClassName.class.getClassLoader();

THE END!!!

95310cookie-checkAndroid – Implement Parcelable to pass an object via BundleAndroid – Implement Parcelable to pass an object via Bundle

Related posts

2 Thoughts to “Android – Implement Parcelable to pass an object via Bundle

  1. […] Android – Implement Parcelable to pass an object via Bundle […]

  2. […] can read this post to learn how to implement the Parcelable […]

Comments are closed.