I would like to have a behavior similar to YouTube:
Here are my constraints:
Toolbar
(inside an AppBarLayout
if that matters)BottomNavigationView
)Toolbar
up buttonBy default, when all is implemented, the navigation is handled automatically by the Jetpack Navigation library. I hook the toolbar with the navigation controller using this helper:
NavigationUI.setupWithNavController(main_toolbar, navController)
This works well, but if I switch one of the bottom tabs, the up button appears and pressing it pops back to the default destination.
This is fine when bottom tabs are not used, but this is awkward when they are.
You can specify AppBarConfiguration
as a third param for NavigationUI.setupWithNavController
. And pass top level destinations to it, like this:
Set<Integer> topLevelDestinations = new HashSet<>();
topLevelDestinations.add(R.id.navigation_home);
topLevelDestinations.add(R.id.navigation_trending);
topLevelDestinations.add(R.id.navigation_subscriptions);
topLevelDestinations.add(R.id.navigation_inbox);
topLevelDestinations.add(R.id.navigation_library);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration
.Builder(topLevelDestinations)
.build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
More details: AppBarConfiguration