Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
L-Andrade opened this issue Mar 7, 2024 · 1 comment

Comments

@L-Andrade
Copy link

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
Copy link

Mojzi commented Mar 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants