Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

611
Visualizações
Parcelable with inheritance in Kotlin- Base class properties not getting parcelized

I have a parent class in Kotlin like this

open class Parent(
var name: String,
var dose: JsonElement?,
val other: String?) {
constructor(name: String, dose: JsonElement?)
    : this(
    name = name.toLowerCase(),
    dose = dose, 
    other = null
)
}

And its child class like this

class Child(
val type: String,
name: String,
dose: JsonElement?
 ) : Parent(name, dose), Parcelable {

constructor(parcel: Parcel) : this(
    parcel.readString() ?: "",
    parcel.readString() ?: "",
    Gson().fromJson(parcel.readString(), JsonElement::class.java))

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeString(type)
    parcel.writeString(name)
    parcel.writeString(Gson().toJson(dose))
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Child> {

    override fun createFromParcel(parcel: Parcel): Child {
        return Child(parcel)
    }

    override fun newArray(size: Int): Array<Child?> {
        return arrayOfNulls(size)
    }
}
}

My problem here is when I pass object of child class as parcelable only child class properties are propagated. All base class properties like name and dose are null after object is de-serialized.

I have cross-verified order of reading and writing and also verified that values are being written correctly at the time of serialization. Let me know if more information is needed from my side.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda