How to Implement the Parcelable in Android?

This article is about the Parcelable interface in Android. I will start with a short definition of it and then implement it using Java and Kotlin. In the end, I will show you how to add it as a plugin in the android studio. So let’s start with this article 👏🏻 …

What is Parcelable?

With the help of Parcelable, we will be able to pass object references between components of the application such as activities using the intent. However, there is another approach called the Serialization which will be enabled when a class implements the Serializable interface, but it uses reflection and allocates more temporary objects leading to a periodic garbage collection. Hence, the Serialization process is slower than the Parcelable.

Parcelable Implementation Using Java

At the first step, your model class which in my case is a class (with 3 attributes) has to implement Parcelable, but then you will get some warnings and must implement the suggested methods which are and .

Then still you will get another warning which says:

Your model class implements Parcelable, but does not provide a CREATOR field.

You should click on the suggested solution which is:

Add Parcelable Implementation.

Then it will add firstly, a protected constructor which takes a Parcelable as a parameter and secondly, a static final field called which has our model class as its type and uses that new constructor to create our model class object().

Now I want to pass a object from an activity called to another one, using intent as below:

Then inside , we can access the passed object:

Parcelable Implementation Using Kotlin

If you are using Kotlin, there is a standard way just as Parcelable implementation using Java which will be looking like the below snippet code:

But, there is another way which creates less boilerplate code called Parcelize that has been introduced in the Kotlin 1.1.4 release. Firstly add the following snippet to your application file:

android {
...
androidExtensions {
experimental = true
}
}

Then add the annotation and Parcelable supertype to your model class:

Then we are going to pass a object from to :

Inside MainActivity
Inside ResultActivity

And we are done 🤩. Notice that all the required code for the Parcelable implementation is generated by the Annotation Processor and it will get updated automatically without the need to change the model class each time.

Parcelable Implementation Using Plugins

There are plugins such as android-parcelable-intellij-plugin that can be imported into Android Studio which helps us to generate the related codes for creating Parcelable. Follow the below path inside Android Studio and then install it: :

Then by pressing the + keys (in Windows OS) inside your model classes, you can find and select the plugin resulting in generating all the relevant codes for Parcelable implementation:

A self-learner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store