Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore fields when using @Parcelize annotation in Kotlin

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?

like image 934
PhillyTheThrilly Avatar asked May 12 '20 18:05

PhillyTheThrilly


1 Answers

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.

like image 164
Nicolas Avatar answered Nov 08 '22 09:11

Nicolas