How to share a viewmodel between two or more Jetpack composables inside a Compose NavGraph?

You can to pass your top viewModelStoreOwner to each destination directly passing to .viewModel() call, composable(“first”) in my example overriding LocalViewModelStoreOwner for the whole content, so each composable inside CompositionLocalProvider will have access to the same view models, composable(“second”) in my example val viewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { “No ViewModelStoreOwner was provided via LocalViewModelStoreOwner” } val … Read more

Jetpack Compose Navigation loads screen infinitely

I re-implemented your posted code with 2 screens, HomeScreen and SettingScreen and stripped out some part of the UiState class and its usages. The issue is in your HomeScreen composable, not in the StateFlow emission. You have this mutableState val uiState by viewModel.uiState.collectAsStateWithLifecycle( initialValue = UiState.Speak ) that is being observed in one of your … Read more