You are on page 1of 1

Intents and Parcels

Open TaskList.kt and change the class declaration so it implements the Parcelable
interface:

Parcelable lets you break down your object into types the Intent system is already
familiar with: strings, ints, floats, Booleans, and other objects which conform to
Parcelable. You can then put all of that information into a Parcel.

To help transfer data, Intents use a Bundle object which can contain Parcelable
objects. This is exactly what you’re using to pass the list as an Extra in the Intent
you set up earlier.

Next, you need to implement some required methods so your object can be parceled
up. Add the following constructor and methods inside the braces of the TaskList
class:

//1
constructor(source: Parcel) : this(
source.readString()!!,
source.createStringArrayList()!!
)

override fun describeContents() = 0

//2
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(name)
dest.writeStringList(tasks)
}

// 3
companion object CREATOR: Parcelable.Creator<TaskList> {
// 4
override fun createFromParcel(source: Parcel): TaskList =
TaskList(source)
override fun newArray(size: Int): Array<TaskList?> =
arrayOfNulls(size)
}

You might also like