Parcels (the Android variety)

Been reading up on Parcelable objects, as CategorizationFragment.java 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) {
            this.name = 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.writeString(name);
            parcel.writeInt(selected ? 1 : 0);
        }
    }

I found myself unfamiliar with the Parcelable interface, but fortunately I found a helpful tutorial at https://guides.codepath.com/android/using-parcelable 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:

categories_suggestion

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 http://www.developerphil.com/parcelable-vs-serializable/ is about 10x slower.

Parcels (the Android variety)

Accepted!!

I’ve been officially accepted into the Outreachy Dec ’15 internship! 🙂 Am totally psyched, can’t wait to start on my project. There is huge room for improvement in my Android development skills, so it’s great to have the opportunity to spend this summer brushing up on those skills and (hopefully!) making some really useful contributions to the app I’m working on.

Stay tuned…

Accepted!!