Aaron Waller
09/09/2022, 6:51 PMIan Lake
09/10/2022, 12:03 AMIan Lake
09/10/2022, 12:05 AMselected
to know whether to trigger a navController.popBackStack("home", inclusive = false)
or a navigate
callAaron Waller
09/10/2022, 7:31 AMselected = currentRoute == item.route
I want to to check if those two routes are on the same NavGraph “HomeGraph”.
So for example “FeedScreen” and “PostCommentScreen” are both inside “HomeGraph”Stylianos Gakis
09/10/2022, 8:32 AMIan Lake
09/10/2022, 1:55 PMhierarchy
call in the code I linked is looking at the nested graph hierarchy - that's how you tell what graph the current destination is inIan Lake
09/10/2022, 1:57 PMIan Lake
09/10/2022, 1:58 PMAaron Waller
09/10/2022, 9:42 PMAaron Waller
09/10/2022, 10:56 PMcomposable(
"communityProfile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {
...
}
Now that I want to keep the bottomNavBar Icon selected I would have to put this code in every NavGraph?
For example if the user is on “feed” which is a sub route of “HomeGraph” and now he navigates to “communityProfile/$id” it should still have the Home Icon selected.
But he can also navigate to “communityProfile/$id” from the ExploreGraph, in this case it should keep the Explore Icon in the BottomNavBar selected.
I’m bad at explaining but I hope you go what I’m trying to sayIan Lake
09/10/2022, 10:59 PMIan Lake
09/10/2022, 11:07 PMAaron Waller
09/10/2022, 11:28 PMStylianos Gakis
09/11/2022, 12:09 AMShowDetail
screen both coming from a bottom nav screen named Home
and from one named Shows
where the route would be home/show/{showId}
and shows/show/{showId}
respectively. That would mean that you would still be able to derive which bottom nav item is highlighted from the hierarchy
.
See an example of this being done in the tivi repo.
Ian do you have any opinions on this approach? I don’t think I’ve seen you discuss it in here before, but may have just missed it.Ian Lake
09/11/2022, 12:20 AMhierarchy
at all - nothing in the structure of the actual graph (the NavDestination
class and its parent
NavGraph
) is being used there. It is essentially creating a second source of truth when one is already available to you as part of how you structure your graph (which also means you could change one and forget to change the other). Note that when you get to deep linking to a destination, the only thing that actually matters in building the correct synthetic back stack is the actual structure of your graph, which mean you really, really need to structure your graph correctly to make sure that back stack is correctIan Lake
09/11/2022, 12:23 AMdefaultValue
as per the previous discussion: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1628542716119200?thread_ts=1628541488.118700&cid=CJLTWPH7SIan Lake
09/11/2022, 12:34 AMcommunityProfile
destination should actually be located in the nested graph associated with the tab that he wants selected when you deep link to that destination - e.g., if you want it to be Home -> community profile, it should be in the home graphStylianos Gakis
09/11/2022, 12:50 AMtrue
or false
would make more sense. Both sound reasonable to me, depending on what the app usually likes to show.Ian Lake
09/11/2022, 12:53 AMIan Lake
09/11/2022, 12:57 AMrememberSaveable
might not be...unless all of your deep links are to your first tab of course 😄)Aaron Waller
09/14/2022, 1:00 AMBottomNavigation {
var selectedIndex by rememberSaveable { mutableStateOf(0) }
tabs.forEachIndexed { index, tab ->
BottomNavigationItem(
selected = selectedIndex == index,
onClick = {
if (selectedIndex == index) {
(navController.findDestination(tab.route) as? NavGraph)?.let {
navController.popBackStack(it.startDestinationId, false)
}
} else {
navController.navigate(tab.route) {
popUpTo(navController.graph.id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
selectedIndex = index
}
},
icon = {
Icon(imageVector = tab.icon, contentDescription = null)
},
label = {
Text(text = tab.label)
},
)
}
}
How can I programatically navigate from HomeGraph to ProfileGraph without pressing the Profile Item on my BottomNavBar?
navController.navigate(BottomNavigationItem.ProfileGraph.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
This does navigate to the ProfileGraph but within the HomeGraph, so it keeps the Home Icon selected.
This is not the behaviour I would’ve expected 🤔Ian Lake
09/14/2022, 1:07 AMIan Lake
09/14/2022, 3:36 AM