17

I want to ignore a field when using the @Parcelize annotation in Kotlin so that the field is not parceled, since this field does not implement the Parcelable interface.

Starting with this, we get an error because PagedList is not parcelable:

@Parcelize
data class LeaderboardState(
    val progressShown: Boolean = true,
    val pagedList: PagedList<QUser>? = null
) : Parcelable

Gives:

Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'

Marking as @Transient gives the same error as above:

@Parcelize
data class LeaderboardState(
    val progressShown: Boolean = true,

    //Same error
    @Transient
    val pagedList: PagedList<QUser>? = null
) : Parcelable

There is an undocumented annotation I found called @IgnoredOnParcel which gives the same error, and a lint error on the annotation:

@Parcelize
data class LeaderboardState(
    val progressShown: Boolean = true,

    //Same error plus lint error on annotation
    @IgnoredOnParcel
    val pagedList: PagedList<QUser>? = null
) : Parcelable

The lint error in that case is: @IgnoredOnParcel' is inapplicable to properties declared in the primary constructor

Is there really no way to do this with @Parcelize?

1 Answer 1

22

Use a regular class and move the property out of the primary constructor:

@Parcelize
class LeaderboardState(
    val progressShown: Boolean = true,
    pagedList: PagedList<QUser>? = null
) : Parcelable {

    @IgnoredOnParcel
    val pagedList: PagedList<QUser>? = pagedList
}

This is apparently the only solution. Make sure to override equals, hashCode, toString, copy, etc as you need them because they won't be defined for a regular class.

EDIT: Here's another solution so you don't lose the features of the data class and you don't lose the automatic parcelization. I'm using a general example here.

data class Person(
    val info: PersonInfo
    val items: PagedList<Item>? = null)

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

You save only Person.info and recreate it from that.

7
  • Thanks, it does seem this is the only solution. Not really ideal as now I have to implement all of those methods I get for free with a data class. It would be easier to just implement my own parcelable implementation instead, so it seems @Parcelize isn't really useful for a data class in this case Commented May 12, 2020 at 20:01
  • @PhillyTheThrilly I added another solution if you're interested.
    – Nicolas
    Commented May 12, 2020 at 20:23
  • thank you, that is a pretty good workaround. For now I am just moving the PagedList out of the data class constructor and making it a var so I can set it without needing the copy method. Your solution is cleaner Commented May 12, 2020 at 20:48
  • your first suggestion doesn't seem to be valid as you're forced to declare each constructor parameter as either val or var
    – takecare
    Commented Jan 31, 2021 at 17:22
  • That's only true for a data class, not a normal class.
    – Nicolas
    Commented Jan 31, 2021 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.