NavigationUI

open class NavigationUI
kotlin.Any
   ↳ androidx.navigation.ui.NavigationUI

Class which hooks up elements typically in the 'chrome' of your application such as global navigation patterns like a navigation drawer or bottom nav bar with your NavController.

Summary

Public methods
open static Boolean
onNavDestinationSelected(item: MenuItem, navController: NavController)

Attempt to navigate to the NavDestination associated with the given MenuItem.

open static Boolean
navigateUp(drawerLayout: DrawerLayout?, navController: NavController)

Handles the Up button by delegating its behavior to the given NavController.

open static Unit
setupActionBarWithNavController(activity: AppCompatActivity, navController: NavController)

Sets up the ActionBar returned by AppCompatActivity#getSupportActionBar() for use with a NavController.

open static Unit
setupActionBarWithNavController(activity: AppCompatActivity, navController: NavController, drawerLayout: DrawerLayout?)

Sets up the ActionBar returned by AppCompatActivity#getSupportActionBar() for use with a NavController.

open static Unit
setupWithNavController(navigationView: NavigationView, navController: NavController)

Sets up a NavigationView for use with a NavController.

open static Unit
setupWithNavController(bottomNavigationView: BottomNavigationView, navController: NavController)

Sets up a BottomNavigationView for use with a NavController.

Public methods

onNavDestinationSelected

open static fun onNavDestinationSelected(item: MenuItem, navController: NavController): Boolean

Attempt to navigate to the NavDestination associated with the given MenuItem. This MenuItem should have been added via one of the helper methods in this class.

Importantly, it assumes the menu item id matches a valid NavDestination#getAction(int) or NavDestination#getId() to be navigated to.

Parameters
item MenuItem: The selected MenuItem.
navController MenuItem: The NavController that hosts the destination.
Return
Boolean: True if the NavController was able to navigate to the destination associated with the given MenuItem.

navigateUp

open static fun navigateUp(drawerLayout: DrawerLayout?, navController: NavController): Boolean

Handles the Up button by delegating its behavior to the given NavController. This should generally be called from AppCompatActivity#onSupportNavigateUp().

If you do not have a DrawerLayout, you should call NavController#navigateUp() directly.

Parameters
drawerLayout DrawerLayout?: The DrawerLayout that should be opened if you are on the topmost level of the app.
navController DrawerLayout?: The NavController that hosts your content.
Return
Boolean: True if the NavController was able to navigate up.

setupActionBarWithNavController

open static fun setupActionBarWithNavController(activity: AppCompatActivity, navController: NavController): Unit

Sets up the ActionBar returned by AppCompatActivity#getSupportActionBar() for use with a NavController.

By calling this method, the title in the action bar will automatically be updated when the destination changes (assuming there is a valid NavDestination#getLabel).

The action bar will also display the Up button when you are on a non-root destination. Call #navigateUp(DrawerLayout, NavController) to handle the Up button.

Parameters
activity AppCompatActivity: The activity hosting the action bar that should be kept in sync with changes to the NavController.
navController AppCompatActivity: The NavController that supplies the secondary menu. Navigation actions on this NavController will be reflected in the title of the action bar.

setupActionBarWithNavController

open static fun setupActionBarWithNavController(activity: AppCompatActivity, navController: NavController, drawerLayout: DrawerLayout?): Unit

Sets up the ActionBar returned by AppCompatActivity#getSupportActionBar() for use with a NavController.

By calling this method, the title in the action bar will automatically be updated when the destination changes (assuming there is a valid NavDestination#getLabel).

The action bar will also display the Up button when you are on a non-root destination and the drawer icon when on the root destination, automatically animating between them. Call #navigateUp(DrawerLayout, NavController) to handle the Up button.

Parameters
activity AppCompatActivity: The activity hosting the action bar that should be kept in sync with changes to the NavController.
navController AppCompatActivity: The NavController whose navigation actions will be reflected in the title of the action bar.
drawerLayout AppCompatActivity: The DrawerLayout that should be toggled from the home button

setupWithNavController

open static fun setupWithNavController(navigationView: NavigationView, navController: NavController): Unit

Sets up a NavigationView for use with a NavController. This will call #onNavDestinationSelected(MenuItem, NavController) when a menu item is selected. The selected item in the NavigationView will automatically be updated when the destination changes.

Parameters
navigationView NavigationView: The NavigationView that should be kept in sync with changes to the NavController.
navController NavigationView: The NavController that supplies the primary and secondary menu. Navigation actions on this NavController will be reflected in the selected item in the NavigationView.

setupWithNavController

open static fun setupWithNavController(bottomNavigationView: BottomNavigationView, navController: NavController): Unit

Sets up a BottomNavigationView for use with a NavController. This will call #onNavDestinationSelected(MenuItem, NavController) when a menu item is selected. The selected item in the BottomNavigationView will automatically be updated when the destination changes.

Parameters
bottomNavigationView NavigationView: The BottomNavigationView that should be kept in sync with changes to the NavController.
navController NavigationView: The NavController that supplies the primary menu. Navigation actions on this NavController will be reflected in the selected item in the BottomNavigationView.