How to Implement and Use a Parcelable Class in Android: Part 1

Estefania Cassingena Navone
Techmacademy
Published in
5 min readApr 14, 2018

Welcome! First of All… We need some Context!

Creating a Parcelable class is a vital skill for Android Developers because it allows you to pass an object from one Activity to another. This series will walk you through step by step in the process of implementing a parcelable class and using it in a simple App. Let’s get started! 👍

Real-World Scenarios 🌐

To give some you context of why we need to learn how to make class parcelable, I will use a scenario I faced during my Android Developer Nanodegree when I developed an App that displays data for popular movies from The MovieDB API.

  • When I made a request to the API, Movie objects were created. The Main Activity displayed all the posters and ratings of the movies but if the user clicked on a movie poster, a new Activity had to be launched to display the movie’s data.
  • I had already made an API request and in order to avoid making another one I made the class Movie a parcelable class. This way I could efficiently transfer the object I had created in the Main Activity to the Details Activity.

Parcelable Behind the Scenes!

When an object is parcelable, we can pass it to another Activity. This Activity then recreates the object so we can access its attributes and methods.

Now Let’s go to the Code! 💻

Step 1: Creating the Project

First we create a new Project in Android Studio with MainActivity, DetailsActivity and our Parcelable class, House (Let’s pretend we’re building an app for a real state company)

Project Structure

Step 2: Implementing Parcelable

When we first open the House class, we see the class definition.

We make this class implement Parcelable (It will display a warning until we implement the necessary methods)

💡 Tips: You can implement the methods automatically by clicking Alt + Enter (on Windows) while your cursor is on the code that displays the warning, as shown below:

There are only two methods we need to implement (describeContents and writeToParcel):

After we click OK, we see that there is still a warning being displayed in the Class name:

Press Alt + Enter again (on Windows) to add the Parcelable Implementation

Customizing the Class

We now have our class but we need to add attributes. Notice the first constructor in the class below. You may ask: How can we add attributes to a constructor that now takes a Parcel as argument? Let’s find out!

Custom Attributes

We will first add variables for the attributes:

Class Attributes

💡 Tips: I’ve chosen these attributes to demonstrate how each data type is written and read to and from the parcel).

Custom Constructor

We will add a custom constructor to create instances as we normally do.

Now for the Fun Part, Writing to the Parcel!

When we launch the destination Activity, our object will be written to a Parcel using this method that we already created in the class in the previous steps:

For most data types we use this syntax to write the attributes to the parcel (Android Studio will display the options available):

parcel.write<dataType>(variableOrValue);

💡 Tips: The syntax may vary depending on the data type such as with the Boolean attribute nearSchool . To learn more about how to write and read Booleans in a Parcelable class, please refer to my article: 👍

Now Let’s Read from the Parcel!

The constructor below is the one we will use to read from the parcel when our object is received in the destination Activity. Have you noticed that it takes a Parcel as a parameter? This is the parcel we will read from.

The general syntax to read from the parcel is:

in.read<dataType>();

⚠️ WARNING: It is extremely important to highlight that you must read the values in the same order that they were written inwriteToParcel⚠️

This is our finished Class! 👍

💡 Tips: You can automatically create getters and setters in Android Studio by Clicking on Alt + insert or by going to code -> Generate

Continue to Part 2 👋

In the next article of this series we will cover How to pass a parcelable object between Activities using a very simple Android App as demo. See you there! 😃

I really hope you found this article helpful. If you have, please 👏 to help others embarking on this journey find this article 😃 . Thank you very much in advance!

--

--

Estefania Cassingena Navone
Techmacademy

Udemy Instructor | Developer | MITx 6.00.1x Community TA | Writer @Medium | CS & Mathematics Student | WTM Udacity Scholar