Keshav Aggarwal
Keshav Aggarwal

Reputation: 763

Read List of List from Parcelable in Kotlin constructor android

I want to basically read the List of List from the Parcel in Kotlin constructor, I have my class structured like this:

data class Sports(
    var media: List<List<MediaObject>>?,
    var likes: Int) : Parcelable {
constructor(parcel: Parcel) : this(
        TODO("media"),
        parcel.readInt(),
)

override fun writeToParcel(parcel: Parcel, flags: Int) {
    if (media == null || media!!.isEmpty()) {
        parcel.writeInt(0)
    } else {
        parcel.writeInt(media!!.size)
        for (mediaObjects in media!!) {
            parcel.writeTypedList(mediaObjects)
        }
    }
    parcel.writeInt(likes)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Sports> {
    override fun createFromParcel(parcel: Parcel): Sports {
        return Sports(parcel)
    }

    override fun newArray(size: Int): Array<Sports?> {
        return arrayOfNulls(size)
    }
}}

I have a List<List<MediaObject>> which i want to read from the parcel, how can we achieve this, i guess we can have some inline functions but not sure how we'll do this ?

Kotlin automatically adds TODO if its List<List<>> type object.

Upvotes: 2

Views: 1871

Answers (1)

Taras Parshenko
Taras Parshenko

Reputation: 604

In Kotlin 1.1.4 was added Parcelable support with @Parcelize annotation

It's more convenient way to deal with it

Check it out:

https://kotlinlang.org/docs/tutorials/android-plugin.html#parcelable

Upvotes: 2

Related Questions