Identify a common destination for several UI elements

You can use a global action to identify a common destination that can be accessed by several UI elements. For example, you might want a Cancel button on several different destinations to navigate to the same main app screen.

Create a global action

To create a global action:

  1. From the Graph Editor, click on a destination to highlight the destination.
  2. Right-click on the destination to display the context menu.
  3. Select Add Action > Global. An arrow () appears to the left of the destination.
  4. Click Text tab to navigate to the XML text view. The XML for the global action looks similar to the following.
<action android:id="@+id/action_global_mainFragment"
    app:destination="@id/mainFragment"/>

Use a global action

To use a global action in your code, pass the resource ID of the global action to the navigate() method for each UI element:

Kotlin

viewTransactionButton.setOnClickListener { view ->
    view.findNavController().navigate(R.id.action_global_mainFragment)
}

Java

viewTransactionsButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       Navigation.findNavController(view).navigate(R.id.action_global_mainFragment);
   }
});