Hello, how do you keep state of bottom tabs? Whene...
# compose
n
Hello, how do you keep state of bottom tabs? Whenever I switch back and forth between tabs, everything is recreated and recalled. Code and video in thread
ViewModel is re-triggered every time I enter the section
Copy code
fun navigateToBottomBarRoute(route: String) {
    if (route != currentRoute) {
        navController.navigate(route) {
            launchSingleTop = true
            restoreState = true
            // Pop up backstack to the first destination and save state. This makes going back
            // to the start destination when pressing back in any other bottom tab.
            popUpTo(navController.graph.findStartDestination().id) {
                saveState = true
            }
        }
    }
}
Copy code
private fun NavGraphBuilder.btAppGraph(appState: BTAppState) {
    composable(Screen.Splash.route) {
        SplashScreen { isLoggedIn ->
            val route = if (isLoggedIn) Screen.Home.route else Screen.Login.route
            appState.clearAndNavigate(route)
        }
    }
    composable(Screen.Login.route) {
        LoginScreen { appState.navigateAndPopUp(Screen.Home.route, Screen.Login.route) }
    }
    navigation(
        route = Screen.Home.route,
        startDestination = HomeSections.Tasks.route
    ) {
        addHomeGraph(
            onLogout = { appState.navigateAndPopUp(Screen.Login.route, Screen.Home.route) }
        )
    }
}
Copy code
fun NavGraphBuilder.addHomeGraph(
    onLogout: () -> Unit
) {
    composable(HomeSections.Tasks.route) {
        TaskListScreen()
    }
    composable(HomeSections.TaskApproval.route) {
        TaskListScreen()
    }
    composable(HomeSections.Profile.route) {
        ProfileScreen(onLogout = onLogout)
    }
}
Copy code
bottomBar = {
    if (appState.shouldShowBottomBar) {
        BTBottomBar(
            tabs = appState.bottomBarTabs,
            currentRoute = appState.currentRoute!!,
            navigateToRoute = appState::navigateToBottomBarRoute
        )
    }
},
I scope my VM to
hiltViewModel()
d
What do you mean exactly by keep state?
i
Your start destination needs to actually be your home screen, not a splash screen or login screen: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1654796230717739?thread_ts=1654793995.106589&cid=CJLTWPH7S
n
ohh it really worked thank you! But how can I have the same flow as I had before? I read the thread but didn’t understand how to start with Splash/Login(conditional) first anyway?
ahh okay I moved all composables inside homeGraph and it didn’t work again
Copy code
@Composable
@ReadOnlyComposable
fun resources(): Resources {
    LocalConfiguration.current
    return LocalContext.current.resources
}

private fun NavGraphBuilder.btAppGraph(appState: BTAppState) {
    navigation(
        route = Screen.Home.route,
        startDestination = HomeSections.Tasks.route
    ) {
        composable(Screen.Splash.route) {
            SplashScreen { isLoggedIn ->
                val route = if (isLoggedIn) Screen.Home.route else Screen.Login.route
                appState.clearAndNavigate(route)
            }
        }
        composable(Screen.Login.route) {
            LoginScreen { appState.navigateAndPopUp(Screen.Home.route, Screen.Login.route) }
        }
        addHomeGraph(
            onLogout = { appState.navigateAndPopUp(Screen.Login.route, Screen.Home.route) }
        )
    }
}

NavHost(
    modifier = Modifier.padding(it),
    navController = appState.navController,
    startDestination = Screen.Home.route
) { btAppGraph(appState) }
does this sound ok?