Add an Up action

Users need an easy way to get back to your app's main screen. To do this, provide an Up button on the app bar for all activities except the main one. When the user selects the Up button, the app navigates to the parent activity.

This page shows you how to add an Up button to an app bar using the Jetpack Navigation component. For a more detailed explanation, see Update UI components with NavigationUI.

Configure your app bar

Configure your app bar using an AppBarConfiguration. From the AppBarConfiguration, you can inform the app bar of your top-level destinations. If the navigation drawer is configured, the drawer menu icon displays on the app bar on top-level destinations. If the navigation drawer isn't configured, the navigation button is hidden on top-level destinations.

In both cases, the Up button displays on all other destinations. Pressing the Up button calls navigateUp().

The following example shows how to configure an app bar using AppBarConfiguration:

Kotlin

  override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val navController = findNavController(R.id.nav_host_fragment_activity_main)
    
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
        )
    )
    binding.myToolbar.setupWithNavController(navController, appBarConfiguration)
  }
  

Java

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      ...
      NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);

      AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
              R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
              .build();
      NavigationUI.setupWithNavController(binding.myToolbar, navController, appBarConfiguration);
  }