3

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
  • I guess you need to make parent class parcelable. – Rujul Gandhi Jan 31 '19 at 07:05
  • 1
    I have tried it, it doesn't work. Moreover we are not allowed to call super from secondary constructors in kotlin – Abhishek Bansal Jan 31 '19 at 07:14
  • I have tried, and its working fine on my end – Rujul Gandhi Jan 31 '19 at 07:59
  • The child class looks fine (and no, you don't need to make the parent parcelable). However, this `Parent` class doesn't compile, since its secondary constructor has the same parameters as the primary constructor. So I'd guess you are using a different definition of `Parent` and if you look at it you'll find it fails to initialize properties. – Alexey Romanov Jan 31 '19 at 08:00
  • @AlexeyRomanov ah! I had simplified classes a bit, I have different parameters in primary and secondary constructor. Let me try to edit code in question – Abhishek Bansal Jan 31 '19 at 08:06
  • @AlexeyRomanov done – Abhishek Bansal Jan 31 '19 at 08:09
  • @AbhishekBansal Then I am afraid I can't help, this looks to me like it should work. Actually, it should be impossible for `name` to be `null` here, even if you somehow read a wrong parcel. – Alexey Romanov Jan 31 '19 at 09:12
  • no problem @AlexeyRomanov thanks for your effort, this is some super weird thing. I actually tried to reproduce exact same thing in a separate project and it just works fine there. Even when I used same original classes, Kotlin version plugin version. I am now resorting to full `Gson` serialization and deserialization directly in `Intent` bundles. – Abhishek Bansal Jan 31 '19 at 10:30

0 Answers0