4

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?

  • Sounds like a bug in `SavedStateRegistry`. They get a `Bundle` but they don't set the correct class loader. Reading the source code, the only way I can think of is to wait for it to get restored and then hijack the restored bundle via reflection to call `setClassLoader` on it. – EpicPandaForce May 28 '20 at 06:02
  • I wonder whether this could have something to do with the type being `@RawValue` in the `MyViewState.Success` sealed class? It does seem to work fine if the inner `data` list is empty – northpine64 May 28 '20 at 14:01
  • did you get any useful answers? – Keyvan Norouzi Feb 03 '22 at 10:44

0 Answers0