Danish Ansari
03/16/2023, 3:08 PMprivate companion object
which implements Parceler<T>
as described in the documentation here https://developer.android.com/kotlin/parcelize (See example above Supported types section), but it seems like custom serialization logic is not working, I also tried debugging but the breakpoint is also not getting hit inside T.write()
and create()
Example:
sealed class User : Parcelable {
var addressPinCode: String? = null
}
@Parcelize
data class Employee(val employeeName: String, val employeeID: Int) : User() {
private companion object : Parceler<Employee> {
override fun create(parcel: Parcel): Employee {
// This function is not getting called
val employee = Employee(employeeName = parcel.readString()!!, employeeID = parcel.readInt()).apply {
addressPinCode = parcel.readString()
}
return employee
}
override fun Employee.write(parcel: Parcel, flags: Int) {
// This function is also not getting called
parcel.writeString(employeeName)
parcel.writeInt(employeeID)
parcel.writeString(addressPinCode)
}
}
}
Did I miss something from the documentation?
I made a mistake, check thread đź§µDanish Ansari
03/16/2023, 3:35 PMStudent
I have written serialization logic inside Employee
but I was trying to pass instance of Student
between 2 activities and in the receiving activity I was casting the instance as User
Danish Ansari
03/16/2023, 3:36 PM