adrielcafe / voyager

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

Voyager Screen with a `ViewModel` does not have its `SavedStateHandle` saved on process death #356

Open L-Andrade opened 2 months ago

L-Andrade commented 2 months ago

In a Voyager screen with a ViewModel, if we set anything in the SavedStateHandle it won't be restored when process death happens.

Here's a demo project with the issue:

@HiltAndroidApp
class App : Application()

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            VoyagerDemoTheme {
                SlideNavigator(InitialScreen())
            }
        }
    }
}

@Composable
private fun SlideNavigator(navScreen: Screen) {
    Navigator(navScreen) { navigator ->
        SlideTransition(navigator)
    }
}

class InitialScreen : Screen {
    override val key: ScreenKey = uniqueScreenKey

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        Button(onClick = { navigator.push(Screen1()) }) {
            Text(text = "Push new Screen")
        }
    }
}

class Screen1 : Screen {
    override val key: ScreenKey = uniqueScreenKey

    @Composable
    override fun Content() {
        val viewModel = getViewModel<OneViewModel>()
        val counter by viewModel.counter.collectAsState()
        Column {
            Text(text = "Counter: $counter")
            Button(onClick = viewModel::increment) {
                Text(text = "Increment")
            }
        }
    }
}

@HiltViewModel
class OneViewModel @Inject constructor(
    private val handle: SavedStateHandle,
) : ViewModel() {
    val counter = handle.getStateFlow("COUNTER", 0)

    fun increment() {
        handle["COUNTER"] = counter.value + 1
    }
}

The Voyager & Hilt dependencies are:

val voyagerVersion = "1.0.0"
implementation("cafe.adriel.voyager:voyager-navigator:$voyagerVersion")
implementation("cafe.adriel.voyager:voyager-transitions:$voyagerVersion")
implementation("cafe.adriel.voyager:voyager-hilt:$voyagerVersion")
implementation("com.google.dagger:hilt-android:2.50")
kapt("com.google.dagger:hilt-android-compiler:2.50")

If we increment the counter a couple times, and then perform some configuration changes, it works as expected. The counter is kept and the handle has the correct state.

When process death happens, the navigator state is correct, but the ViewModel's SavedStateHandle is not saved, and the counter is reset to its initial value. Here's a recording of it: savedstatehandle.webm

Not sure if this has been reported in another issue, but I did not find anyone mentioning it. This is quite bothersome as we can't store the user's input in a screen across process death.

Mojzi commented 1 month ago

It looks like it's caused by adding transition animations. Removing { navigator -> SlideTransition(navigator) } makes it restore the state correctly. Also even with transitions, after first process death the app starts already on Screen1, so incrementing counter again and triggering another process death makes it restore the state correctly. Navigating back and forward again causes the bug to reappear.

And it's unrelated to ViewModel, because the bug also happens if you use rememberSaveable.