Closed meet2602 closed 2 months ago
I'm having same issue. On my app, I have a bottom navigation bar, which leads to different screens, each with its own viewModel. Every time I navigate to one of the screens, a new viewModel is created for that screen. This issue is happening either on Android or IOS... with iOS I understand it still needs some work, as the lifecycle is different than Android. But I'm concerned why Android is having this issue as well.
The problem here is in unsupported restoring (de/serialization) of NavHostController
outside of Android. It's tracked in https://github.com/JetBrains/compose-multiplatform/issues/4735
Workaround 1: Use single NavHostController and define nested graphs via NavGraphBuilder.navigation() function. See documentation Workaround 2: Move second rememberNavController() call out of wiped composition.
Closing as duplicate
But I'm concerned why Android is having this issue as well.
It's clearly a separate one. If it doesn't work on Android - it's not an issue for this fork, please report the bug to Google.
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.
Describe the bug I have an issue with a common ViewModel and navigation in my Compose Multiplatform application. The ViewModel is initialized in the common main:
val headlineViewModel = viewModel { HeadlineViewModel() }
I use two NavHost components for navigation:
When navigating from the main screen to the detail screen:
navController.navigate(NewsRouteScreen.NewsDetail.route)
and then back to the main screen:
navController.navigateUp()
the
MainNavGraph
resets to the default start destination (MainRouteScreen.Headline.route
). This causes the ViewModel to be recreated and any navigation state or position within theMainNavGraph
is lost. How can I prevent theMainNavGraph
from resetting and preserve the navigation state when navigating back?Affected platforms
Versions
Additional context I also tried using Koin in Android with
viewModelOf
and on other platforms withsingleOf
, and it works perfectly.