Parcelize in Kotlin

Updated 30 November 2021

Save

In Android, to pass the data from one activity to another activity, we use the Parcelable. The kotlin-parcelize plugin provides a Parcelable implementation generator.

The Android’s Parcelable is an interface on which, the values can be written and read from a Parcel. The implementation of the Parcelable interface allows an object to be passed between the different Android components like, activity and fragment.

Implementation in App Gradle

To include support for Parcelable, add the following Gradle plugin to your app’s build.gradle file:

Implementaion in Model Class

To achieve this your class must implement the Parcelable and override some methods.

When the model class is having a variable list of custom Class then, the used class must also implement the Parcelable interface.

When implementing the constructor for the Parcel and writeToParcel method always make sure the implementation of variable position should be same on the both method.

You can also use the @Parcelize annotation so that you don’t have to override the above methods:

The @Parcelize annotation Instructs the Kotlin compiler to generate the writeToParcel(), and describeContents() methods, also the CREATOR factory class automatically.

You can also check out the official documentation provided by Google click here.

. . . . . . . . .

That’s it from my side for today, thanks for reading it until now. You can check our other android blogs Click Here.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home