Sunil Kumar
09/16/2024, 6:36 AMHristijan
09/16/2024, 6:40 AMAlex Styl
09/16/2024, 6:45 AMSunil Kumar
09/16/2024, 7:00 AMStylianos Gakis
09/16/2024, 7:43 AMwe need to clear some state data from screen viewModel so that next time screen works properlyYou shouldn't have to do anything in a BackHandler for this. You should be getting a brand new ViewModel after you've popped a destination completely and you go back into it. What are you using for navigation? And how are you grabbing your viewmodel there? Is it scoped properly only to that destination's lifecycle?
Sunil Kumar
09/16/2024, 7:46 AMStylianos Gakis
09/16/2024, 7:47 AMSunil Kumar
09/16/2024, 7:48 AMStylianos Gakis
09/16/2024, 7:49 AMcomposable<Screen> { ... }
functionSunil Kumar
09/16/2024, 7:56 AM@Composable
fun SearchRoute(
modifier: Modifier = Modifier,
searchViewModel: SearchViewModel = koinInject(),
) { ... }
Now u mean, i should do it like this?
composable<Screen> {
val searchViewModel: SearchViewModel = koinInject()
searchScreenNavGraph(searchViewModel)
}
Let me try.Stylianos Gakis
09/16/2024, 8:06 AM@Composable
fun SearchRoute(
modifier: Modifier = Modifier,
searchViewModel: SearchViewModel = koinInject(),
) {
LaunchedEffect(searchViewModel) {
Log.d("searchViewModel", "searchViewModel hashcode:${searchViewModel.hashCode()}")
}
...
}
And you go to that screen, let it print something.
Then go back, pop the backstack so this screen leaves the composition completely.
And then you navigate to this screen again. Is the hashCode printed the same one as before?Sunil Kumar
09/16/2024, 8:14 AMStylianos Gakis
09/16/2024, 8:20 AMkoinInject
use koinViewModel
😄
I completely missed that
Replace
searchViewModel: SearchViewModel = koinInject(),
with
searchViewModel: SearchViewModel = koinViewModel(),
Stylianos Gakis
09/16/2024, 8:22 AMmodule {
single<SearchViewModel> { SearchViewModel(get()) }
}
Replace with
module {
viewModel<SearchViewModel> { SearchViewModel(get()) }
}
Stylianos Gakis
09/16/2024, 8:22 AMSunil Kumar
09/16/2024, 8:24 AMSunil Kumar
09/16/2024, 8:25 AMSunil Kumar
09/16/2024, 8:29 AMSunil Kumar
09/16/2024, 8:36 AMagrosner
09/16/2024, 10:14 AMagrosner
09/16/2024, 10:15 AMhiltViewModel(backstackEntry)
to make unique viewmodel per screen instanceIan Lake
09/16/2024, 2:04 PMcomposable
destination since NavHost already sets the LocalViewModelStoreOwner
to that destination (and that's the default value for viewModel
, etc.)