I have a class User
that implements the Parcelable
interface. User
also has a property of type MutableList<String>
. I would like to know how to read/write this type for Parcelable
.
Here's my code so far
data class User(
val contactNumber: String,
val email: String,
val fullName: String,
var instanceToken: String,
val isAdmin: Boolean,
val isValid: Boolean,
val organization: String,
val registrationTokens: MutableList<String>,
val uid: String,
val unitNumber: String
) : Parcelable {
constructor(source: Parcel) : this(
source.readString() ?: "",
source.readString() ?: "",
source.readString() ?: "",
source.readString() ?: "",
source.readBoolean(),
source.readBoolean(),
source.readString() ?: "",
source.readStringList(),
source.readString() ?: "",
source.readString() ?: ""
)
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.let {
it.writeString(contactNumber)
it.writeString(email)
it.writeString(fullName)
it.writeString(instanceToken)
it.writeBoolean(isAdmin)
it.writeBoolean(isValid)
it.writeString(organization)
it.writeStringList(registrationTokens)
it.writeString(uid)
it.writeString(unitNumber)
}
}
For the write part, there's a writeStringList()
method which is convenient. But for the read part, the provided readStringList()
does not actually return MutableList<String>
. It only returns Unit
/ void
. Hence, it spits out a compilation error.
What is the proper way of doing this?
Use createStringArrayList()
. readStringList
is intended to reuse a single List
instead of creating a new one each time, this isn't what you want here.