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 Task
class (with 3 attributes) has to implement Parcelable, but then you will get some warnings and must implement the suggested methods which are describeContents():int
and writeToParcel(parcel:Parcel,i:int):void
.
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 Creator
which has our model class as its type and uses that new constructor to create our model class object(Task
).
Now I want to pass a Task
object from an activity called MainActivity
to another one, ResultActivity
using intent as below:
Then inside ResultActivity
, 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 build.gradle
file:
android {
...
androidExtensions {
experimental = true
}
}
Then add the @Parcelize
annotation and Parcelable supertype to your model class:
Then we are going to pass a Task
object from MainActivity
to 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: File
➛ Settings
➛ Plugins
➛ Marketplace
:
Then by pressing the alt
+ insert
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:
And that was all for this article 😊. I hope it was useful for you. Do not hesitate to share your ideas.