Parcels (the Android variety)

Been reading up on Parcelable objects, as contains the nested class

public static class CategoryItem implements Parcelable {
        public String name;
        public boolean selected;

        public static Creator<CategoryItem> CREATOR = new Creator<CategoryItem>() {
            public CategoryItem createFromParcel(Parcel parcel) {
                return new CategoryItem(parcel);

            public CategoryItem[] newArray(int i) {
                return new CategoryItem[0];

        public CategoryItem(String name, boolean selected) {
   = name;
            this.selected = selected;

        public CategoryItem(Parcel in) {
            name = in.readString();
            selected = in.readInt() == 1;

        public int describeContents() {
            return 0;

        public void writeToParcel(Parcel parcel, int flags) {
            parcel.writeInt(selected ? 1 : 0);

I found myself unfamiliar with the Parcelable interface, but fortunately I found a helpful tutorial at which states:

Due to Android’s memory management scheme, you will often find yourself needing to communicate with different components of your application, system components, or other applications installed on the phone. Parcelable will help you pass data between these components.

Android uses Binder to facilitate such communication in a highly optimized way. The Binder communicates with Parcels, which is a message container. The Binder marshals the Parcel to be sent, sends and receives it, and then unmarshals it on the other side to reconstruct a copy of the original Parcel.

To allow for your class instances to be sent as a Parcel you must implement the Parcelable interface along with a static field called CREATOR, which itself requires a special constructor in your class.

The article also explains, in detail, the template used to create Parcels. An interesting point of note in the article is that there is a Parcelable plugin for IntelliJ that generates the template for this as well!

So it seems that the CategoryItem nested class in our app is basically a container to pass along the ‘name’ and ‘selected status’ vars to an Adapter that populates the ListView when displaying category suggestions that the user can tick to select, as so:


Also, just realized that the reason the boolean var ‘selected’ is handled so strangely in our writeToParcel() method is that apparently there is no simple way to write a boolean to a Parcel, hence the need for the workaround.

The other method of passing objects between components is using Serializable, which according to is about 10x slower.

Parcels (the Android variety)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s