ViewModel with SavedStateHandle - exception when LiveData with custom data class is being deserialized

Viewed 522

I have a simple ViewModel which gets the SavedStateHandle. This ViewModel has single LiveData that hold MyViewState<List<MyDataClass>>, which is as follows:

sealed class MyViewState<T> : Parcelable {
    @Parcelize data class ErrorState<T>(val error: @RawValue Any?): MyViewState<T>()
    @Parcelize data class SuccessState<T>(val data: @RawValue T): MyViewState<T>()
}

and

@Parcelize data class MyDataClass(val data: String): Parcelable

The LiveData in the ViewModel is retrieved like this:

class MyViewModel(
    private val handle: SavedStateHandle
): ViewModel() {

    var myLiveData = handle.getLiveData<MyViewState<List<MyDataClass>>>("myLiveData")
    ...
}

And this works great. However once the app gets killed and I switch back to it, the app crashes with this exception:

E/Parcel: Class not found when unmarshalling: com.myapp.models.MyDataClass
java.lang.ClassNotFoundException: com.myapp.models.MyDataClass
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at android.os.Parcel.readParcelableCreator(Parcel.java:2811)
        at android.os.Parcel.readParcelable(Parcel.java:2765)
        at android.os.Parcel.readValue(Parcel.java:2668)
        at android.os.Parcel.readListInternal(Parcel.java:3098)
        at android.os.Parcel.readArrayList(Parcel.java:2319)
        at android.os.Parcel.readValue(Parcel.java:2689)
        at com.myapp.core.MyViewState$Success$Creator.createFromParcel(Unknown Source:13)
        ...

...and I can't figure out why would this crash happen and how to solve it.

Any ideas?

0 Answers
Related