Updated 30 November 2021
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.
To include support for Parcelable, add the following Gradle plugin to your app’s build.gradle file:
1 2 3 4 5 |
plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-parcelize' } |
To achieve this your class must implement the Parcelable and override some methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.os.Parcel import android.os.Parcelable class Person() :Parcelable { var name: String ="" var number: String ="" var addresses:ArrayList<Address> = ArrayList() constructor(parcel: Parcel) : this() { name = parcel.readString()?:"" number = parcel.readString()?:"" addresses= parcel.createTypedArrayList(Address.CREATOR)?:ArrayList() } override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(name) parcel.writeString(number) parcel.writeTypedList(addresses) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator<Person> { override fun createFromParcel(parcel: Parcel): Person { return Person(parcel) } override fun newArray(size: Int): Array<Person?> { return arrayOfNulls(size) } } } |
When the model class is having a variable list of custom Class then, the used class must also implement the Parcelable interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.os.Parcel import android.os.Parcelable class Address() : Parcelable { var city:String="" var state:String="" var country: String="" constructor(parcel: Parcel) : this() { city = parcel.readString()?:"" state = parcel.readString()?:"" country = parcel.readString()?:"" } override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(city) parcel.writeString(state) parcel.writeString(country) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator<Address> { override fun createFromParcel(parcel: Parcel): Address { return Address(parcel) } override fun newArray(size: Int): Array<Address?> { return arrayOfNulls(size) } } } |
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:
1 2 3 4 5 6 |
@Parcelize data class Address( val city: String, val state: String, val country: String, ) : Parcelable |
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.