Parcelize annotation not working when inheritance

22 Views Asked by At

I have 3 classes

@Parcelize
open class Parent():Parcelable

data class Child1():Parent

data class Child2():Parent

I want to pass data from one activity to another in container class which is

class Container(){
 var obj:Parent
}

The problem is that even if I set the obj as Child1(or 2) in first activity, in second activity I can't get obj as Child, I can only get as Parent. How get obj as defined type?

1

There are 1 best solutions below

0
Cembora On

Simply mark the Parent class as sealed. This way, you can ensure that the subclasses are known and can be properly handled during parcelization

@Parcelize
sealed class Parent():Parcelable