Crazy Sage
Crazy Sage

Reputation: 482

How to create from parcel with Kotlin @Parcelize?

I'm trying to migrate parcelable model from Java to Kotlin.

My model looks like this:

import kotlinx.parcelize.*

@Parcelize
class SignatureAuthorityModel
    (var id: Int,
    var cloudId: Int,
    var uuid: UUID?): android.os.Parcelable
{
    override fun equals(other: Any?): Boolean
    {
        //equals implementation
    }

    override fun hashCode(): Int
    {
       //some hash implementation
    }

    override fun toString(): String
    {
       //some stringifier implementation
    }
}

I can call writeToParcel for this model, but SignatureAuthorityModel.createFromParcel(parcel) isn't available. Must I write it separately, or must I configure kotlin-parcelize plugin somehow? From kotlin documentation it seems that createFromParcel should also be generated automatically when I use @Parcelize annotation, and that I must write implementation in companion object Creator only if I have some advanced logic. What is the proper way to do it?

Upvotes: 4

Views: 3211

Answers (4)

bosphere
bosphere

Reputation: 418

To retrieve the CREATOR of Parcelable classes annotated by @Parcelize, simply call parcelableCreator with the correct type information. See below for an example:

@Parcelize
data class MyParcelableClass(val someField: String): Parcelable

// getting Creator for this Parcelable
val creator = parcelableCreator<MyParcelableClass>()

Upvotes: 4

Fargonaut
Fargonaut

Reputation: 791

According to this pull request, there is a means of accessing the CREATOR while still using the @Parcelize annotation. I couldn't discover how just reading the PR or docs alone.

However, I did manage to find (in the PR) a pleasant little function that has so far been successful. I copied and pasted it easily into my project. Calling it will return a CREATOR from which you can then call createFromParcel(parcel).

public inline fun <reified T : Parcelable> parcelableCreator(): Parcelable.Creator<T> =
T::class.java.getDeclaredField("CREATOR").get(null) as? Parcelable.Creator<T>
    ?: throw IllegalArgumentException("Could not access CREATOR field in class ${T::class.simpleName}")

Here's an example of its usage in a simple test that parcels, then creates from parcel.

@Test
fun `UserDto parcelizes then unparcelizes`() {
    val parcel = Parcel.obtain()
    inputUserDto.writeToParcel(parcel, 0)
    parcel.setDataPosition(0)

    val userDtoFromParcel = parcelableCreator<UserDto>().createFromParcel(parcel)

    assertThat(inputUserDto).isEqualTo(userDtoFromParcel)
}

Here is a link to exactly where I found the helper inline function. It may not work in every case, but so far it's been good to me.

Upvotes: 3

Ivo
Ivo

Reputation: 23357

It seems to be a known issue that the creator is not accessible when using @Parcelize

See this issue created 5 years ago: https://youtrack.jetbrains.com/issue/KT-19853

Following the comments there it doesn't seem like they are doing anything about it.

My personal opinion is that they probably don't bother about it because for the most common use case it's not necessary. The most common use case for Parcelable in Android development is to pass the objects from one Activity to another using intent.putExtra() or intent.putParcelableArrayListExtra() and in that case the the turning into Parcel and back happens automatically.

EDIT:
hmm.. actually they mention here https://github.com/JetBrains/kotlin/pull/4575 that it should be available using parcelableCreator but I can't figure out how to use it or if it's even actually in the latest version

Upvotes: 0

Mehul Kabaria
Mehul Kabaria

Reputation: 6632

If you are using @Parcelize annotation then you don't need to write createFromParcel method. Because @Parcelize manages createFromParcel method internally.

just write your data class like below. And might be you wrong import for Parcelable and your class should be like below.

import kotlinx.parcelize.Parcelize

@Parcelize
class User(val firstName: String, val lastName: String, val age: Int): Parcelable

Upvotes: 1

Related Questions