Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

609
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!