Parcels and Bundles

From Android Wiki

Revision as of 08:46, 31 January 2013 by Ldo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, searcha

Parcels and Bundles are the two main serialization mechanisms used for interprocess communication in Android. The difference between them is that, in a Parcel, items must be read back in the same sequence they were written, whereas in a Bundle, items are identified by a key string, and so can be read back in any order by specifying the corresponding keys. Parcels are the primary mechanism, but one of the things a Parcel can contain is a Bundle.

Parcelable is an interface that your objects can implement in order to save their state to, and restore it from, a Parcel. The important components of this are

  • A writeToParcel method which can be called to save your object state to a specified Parcel, and
  • A CREATOR static field, which contains a custom instance of Parcelable.Creator, which knows how to recreate your Parcelable object from a specified Parcel, as previously written by your writeToParcel method.

Even though this interface specifically mentions Parcels, Bundles can also contain Parcelable objects.

Personal tools