I have written a custom serialization logic inside...
# android
d
I have written a custom serialization logic inside the
private 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:
Copy code
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 🧵
âś… 1
My bad, I have one more data class
Student
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
typical “I’m an idiot” moment đź«