Parcelable extension for Kotlin is not working as intended - throwing Exception

52 views Asked by At

I want to save my result from API inside SavedStateHandle for my ViewModel. For this reason I made all of my data Parcelable to make this function.

Code:

@Parcelize
data class DataResult(
    val result: Result<List<MyData>>?,
) : Parcelable

@Parcelize
data class MyData(
    val id: String,
    val name: String,
    val phone: String,
    val photo: String? = null): Parcelable
class MyViewModel(savedState: SavedStateHandle): ViewModel(){

     val resultData = savedState.getLiveData<DataResult?>("resultData", null)
     ...
}

Getting exception when I minimize app and go to background:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = kotlin.Result)
Caused by: java.io.NotSerializableException: com.my.project.viewmodel.MyData

I just wrapped Result class in my own Data class and made it Parcelable just in case although Result class is Serializable by default so it should not be a problem at all.

0

There are 0 answers