Main Nav Host
NavHost(
navController = appState.navController,
route = rootRoute,
startDestination = authGraphRoute,
) {
authGraph(appState)
editProfileListScreen(appState, appViewModel)
homeGraph(appState)
}
Auth Nav Graph
const val authGraphRoute = "auth_graph_route"
fun NavGraphBuilder.authGraph(
appState: InnoAppState
) {
navigation(
startDestination = splashRoute,
route = authGraphRoute
) {
splashScreen(appState)
onboardingScreen(appState)
signInScreen(appState)
otpScreen(appState)
notificationPermissionScreen(appState)
appUpdateScreen(appState)
}
}
navigation to editprofileList screen code
appState.navController.navToEditProfileListScreen(){
popUpTo(authGraphRoute){
inclusive = true
}
}
navigation to homegraph from editprofileList
appState.navController.navigateToHomeGraph(){
popUpTo(editProfileListScreenRoute){
inclusive = true
saveState = false
}
launchSingleTop = true
restoreState = true
}
bottom navigation item click navigation code
fun navigateToTopLevelDestination(topLevelDestination: BtmBarScreen) {
val topLevelNavOptions = navOptions {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
when (topLevelDestination) {
BtmBarScreen.HomeGraph -> navController.navigateToHomeGraph(topLevelNavOptions)
BtmBarScreen.MyList -> navController.navToMyWatchListGraph(topLevelNavOptions)
BtmBarScreen.LiveTvGraph -> navController.navigateToLiveTvGraph(topLevelNavOptions)
BtmBarScreen.BrowseGraph -> navController.navigateToBrowseGraph(topLevelNavOptions)
BtmBarScreen.YouGraph -> navController.navToYouGraph(topLevelNavOptions)
}
}
I have other navigation graphs for remaining top level destinations. The problem here is findStartDestination() should return the homeGraphRoute as the result, but it returns splashRoute. so using navOptions on navigateToTopDestination on clicking the bottom navigation item it creates new screen and viewmodel instance every time.