Stay organized with collections
Save and categorize content based on your preferences.
You can use a global action to create a common action that multiple
destinations can use. For example, you might want buttons in
different destinations to navigate to the same main app screen.
A global action is represented in the Navigation Editor by a small arrow that
points to the associated destination, as shown in figure 1.
Figure 1. A global action that leads to a nested
graph.
Create a global action
To create a global action, do the following:
From the Graph Editor, click on a destination to highlight it.
Right-click on the destination to display the context menu.
Select Add Action > Global. An arrow
()
appears to the left of the destination.
Click the Text tab to navigate to the XML text view. The XML for the
global action looks similar to the following:
To use a global action in your code, pass the resource ID of the global action
to the navigate()
method for each UI element, as shown in the following example:
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-12-11 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-11 UTC."],[],[],null,["# Global actions\n\nYou can use a *global action* to create a common action that multiple\ndestinations can use. For example, you might want buttons in\ndifferent destinations to navigate to the same main app screen.\n\nA global action is represented in the Navigation Editor by a small arrow that\npoints to the associated destination, as shown in figure 1.\n**Figure 1.** A global action that leads to a nested graph.\n\nCreate a global action\n----------------------\n\nTo create a global action, do the following:\n\n1. From the **Graph Editor**, click on a destination to highlight it.\n2. Right-click on the destination to display the context menu.\n3. Select **Add Action \\\u003e Global** . An arrow () appears to the left of the destination.\n4. Click the **Text** tab to navigate to the XML text view. The XML for the\n global action looks similar to the following:\n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cnavigation xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n xmlns:tools=\"http://schemas.android.com/tools\"\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:id=\"@+id/main_nav\"\n app:startDestination=\"@id/mainFragment\"\u003e\n\n ...\n\n \u003caction android:id=\"@+id/action_global_mainFragment\"\n app:destination=\"@id/mainFragment\"/\u003e\n\n \u003c/navigation\u003e\n\nUse a global action\n-------------------\n\nTo use a global action in your code, pass the resource ID of the global action\nto the [`navigate()`](/reference/androidx/navigation/NavController#navigate(int))\nmethod for each UI element, as shown in the following example: \n\n### Kotlin\n\n```kotlin\nviewTransactionButton.setOnClickListener { view -\u003e\n view.findNavController().navigate(R.id.action_global_mainFragment)\n}\n```\n\n### Java\n\n```java\nviewTransactionsButton.setOnClickListener(new View.OnClickListener() {\n @Override\n public void onClick(View view) {\n Navigation.findNavController(view).navigate(R.id.action_global_mainFragment);\n }\n});\n```\n\nUse Safe Args with a global action\n----------------------------------\n\nFor information on using Safe Args with global actions, see\n[Pass data between destinations](/topic/libraries/architecture/navigation/navigation-pass-data#safe-args-global).\n\nAdditional resources\n--------------------\n\nTo learn more about navigation, consult the following\nadditional resources.\n\n### Codelabs\n\n- [Navigation Codelab](https://codelabs.developers.google.com/codelabs/android-navigation/index.html?index=..%2F..%2Findex#0)\n\n### Videos\n\n- [Android Jetpack:\n manage UI navigation with Navigation Controller (Google I/O '18)](https://www.youtube.com/watch?v=8GCXtCjtg40)"]]