Suppose we have a certain abstract class Entity:

abstract class Entity { var id: String? = null var health: Int = 0 } 

And the heir to this Monster class:

 @Parcelize data class Monster(var name: String?, var team: String?) : Entity(), Parcelable 

Monster we deserialize using Retrofit2 + GSON from the resulting json'a, which contains the fields id, health, name and team.

The problem is that it is impossible to transfer this object of the Monster class to another activity using IntentExtras, because the fields from the base class Entity are not transferred. That is, the id and health object received in another activity is not initialized.

What can be done in this situation? I had an idea to solve the problem, if you make the default constructor in Entity, and in Monster implement the following construction:

 @Parcelize data class Monster(@SerializedName("id") var idM: String?, @SerializedName("health") var healthM: Int, var name: String?, var team: String?): Entity(idM, healthM), Parcelable 

But then GSON swears, says that there are several fields named id and health. Anyway, a constructor in an abstract class would be inconvenient for me.

Or is it better not to use @Parcelize at all in this situation?

  • one
    Fields are not transferred because they belong to Entity , and it does not implement Parcelable . Kotlin I do not know - so the assumptions: - Try to make an Entity Parcelable or in Monster implement the methods of parcelization with handles as you please. About customization a little bit written in the docks: kotlinlang.org/docs/tutorials/android-plugin.html#parcelable - woesss

0