How to parcelise member variable other than constr

2019-08-02 15:39发布

问题:

I am using Room and Kotlin data class. Such as,

@Entity(tableName = "Person")
@Parcelize
class Test(@ColumnInfo(name = "name") var name:String) : Parcelable{
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "ID")
    var id: Long? = null
}

I can create the instance using the constructor and insert the data. I am also getting a warning "property would not be serialized into a 'parcel'". When I was trying to send the object through a bundle, the id is missing, which is expected as the warning says so. How can I add that member ID in the parcel? I am not keeping the ID in the constructor as I want them to be generated automatically.

Thanks in Advance.

回答1:

You can find this information with the documentation:

@Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.

If your class requires more advanced serialization logic, you can write it inside a companion class:

@Parcelize
data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
    private companion object : Parceler<User> {
        override fun User.write(parcel: Parcel, flags: Int) {
            // Custom write implementation
        }

        override fun create(parcel: Parcel): User {
            // Custom read implementation
        }
    }
}


回答2:

Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.

@Entity(tableName = "Person")
@Parcelize
data class Test(@ColumnInfo(name = "name") var name:String,
           @PrimaryKey(autoGenerate = true)
           @ColumnInfo(name = "ID")
           var id: Long? = null) : Parcelable

And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.