adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.27k stars 109 forks source link

Parcelable encountered IOException writing serializable object #368

Closed stevdza-san closed 2 months ago

stevdza-san commented 2 months ago

Id do have a simple Home/Details screen scenario. A Details screen takes an argument/parameter, that I'm passing from a Home screen. It's a custom object that I'm passing. When I do open the Details screen and pass the argument, I click home button on my smartphone to go outside of the app, and put the app in the background, however as soon as I do that I get the exception: Caused by: java.io.NotSerializableException: domain.quiz.Quiz

Which means when I go back to my app, the app will restart and the splash screen will show up again. The state will not be saved. Any solutions?

jeffreyliu8 commented 1 month ago

what is the solution?

ArleyPereira commented 1 month ago

It was necessary to implement Serializable. Does not work with Parcelable

import java.io.Serializable

data class ValidateCodeParameters(
     val authenticator: String = "",
     val type: String = ""
): Serializable
Syer10 commented 1 month ago

It will work with Parcelable if you make the Screen itself Parcelable as well

ArleyPereira commented 1 month ago

@Syer10 How can I make the screen parcelable?

Syer10 commented 1 month ago

The normal way https://developer.android.com/kotlin/parcelize

ArleyPereira commented 1 month ago

Example of operation, using Parcelable

@Parcelize
data class ValidateCodeParameters(
    val authenticator: String = "",
    val type: String = ""
) : Parcelable
@Parcelize
class ValidateCodeScreen(
    private val parameters: ValidateCodeParameters
) : Screen, Parcelable {

    @Composable
    override fun Content() {
        val viewModel = hiltViewModel<ValidateCodeViewModel>()
        val state by viewModel.state.collectAsState()
        ValidateCodeScreenView(
            parameters = parameters,
            state = state,
            action = viewModel::submitAction
        )
    }
}