How to ignore fields when using @Parcelize annotation in Kotlin

懵懂的女人 提交于 2020-12-11 05:14:08

问题


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:


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.



来源:https://stackoverflow.com/questions/61758963/how-to-ignore-fields-when-using-parcelize-annotation-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!