I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) setSupportActionBar(toolbar) val navController = navHostFragment.findNavController() setupActionBarWithNavController(this, navController) setupWithNavController(bottomNav, navController) } override fun onSupportNavigateUp(): Boolean = findNavController(navHostFragment).navigateUp() }
Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.
I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?
NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.
Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.
onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. ActionMode. onWindowStartingSupportActionMode(ActionMode.Callback callback) Called when a support action mode is being started for this window.
Starting with 1.0.0-alpha07 you can use AppBarConfiguration
to configure that behaviour.
AppBarConfiguration
has a Builder constructor so you can create a new Builder
with a specific set of top level destinations, referenced by their id
(this id
is the one you set on your navigation layout).
Create new AppBarConfiguration
:
val appBarConfiguration = AppBarConfiguration .Builder( R.id.navigationHomeFragment, R.id.navigationListFragment, R.id.navigationProfileFragment) .build()
Then, instead of setupActionBarWithNavController(this, navController)
you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)
This is the right way to handle top navigation behaviours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With