I'm getting different instances of a view model in...
# compose
s
I'm getting different instances of a view model in different screens of the same
NavHost
. Is this intended? How can I scope the view models to the
NavHost
or activity?
Copy code
NavHost(navController = navController, startDestination = "screen1") {
    composable("screen1") { Screen1() }
    composable("screen2") { Screen2() }
}

@Composable fun Screen1() {
    val viewModel: MainViewModel = viewModel()
}

@Composable fun Screen2() {
    val viewModel: MainViewModel = viewModel()
}
Without passing them as arguments
i
Create your ViewModel outside of the NavHost if you want it scoped outside of the individual screen
✔️ 1
g
Do you mean create once and pass trough arguments?
s
Okay thanks Yes @Grigorii Yurkov
a
Are there any example projects that make use of this library yet? Jw
i
@John O'Reilly has updated his apps to use it: https://twitter.com/joreilly/status/1321518153264959489?s=19