when working with `NavigationComponenet` and `BottomNavigation` I have a problem with tabs being sel...
m
when working with
NavigationComponenet
and
BottomNavigation
I have a problem with tabs being selected (code in thread)
Copy code
Scaffold(
            bottomBar = {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentRoute = navBackStackEntry?.arguments?.getString(KEY_ROUTE)
                BottomNavigation(modifier = Modifier.navigationBarsPadding()) {
                    tabs.forEach { tab ->
                        println("${tab.name} | $currentRoute")
                        BottomNavigationItem(
                            modifier = Modifier.navigationBarsPadding(),
                            icon = {
                                Icon(
                                    painter = painterResource(id = tab.icon),
                                    contentDescription = stringResource(id = tab.label),
                                )
                            },
                            label = { Text(text = stringResource(id = tab.label)) },
                            selected = tab.name == currentRoute,
                            onClick = {
                                navController.navigate(tab.name) {
                                    popUpTo = navController.graph.startDestination
                                    launchSingleTop = true
                                }
                            },
                            alwaysShowLabel = false,
                            selectedContentColor = MaterialTheme.colors.secondary,
                            unselectedContentColor = LocalContentColor.current,
                        )
                    }
                }
            }
        )
body of the
Scaffold
is just NavGraph
problem I have is this: when I select first tab
BottomNavigationItem
for that tab is not selected and
currentRoute
is
null
it works great on other tabs but on the first one that is shows it’s not
I imagine it’s because of
pop
behavior I have there but shouldn’t it pop and than put new destination and
currentRoute
?
n
Some time ago someone here posted a solution they were using to solve this problem. I cant really remember who it was and I'm lazy to search for that now 😄 Anyways, here's a piece of code that works for me. https://gist.github.com/nikolaDrljaca/173c47d22b0f74a9c07f31eab786449a
👍 1
Theres some funky code going on to gran the current route
m
Hvala @Nikola Drljaca
n
Nema na cemu @Marko Novakovic 😄