Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Mit einer globalen Aktion können Sie eine gemeinsame Aktion erstellen, die für mehrere Ziele verwendet werden kann. Beispielsweise können Sie Schaltflächen in verschiedenen Zielen so konfigurieren, dass sie zum selben Hauptbildschirm der App führen.
Eine globale Aktion wird im Navigationseditor durch einen kleinen Pfeil dargestellt, der auf das zugehörige Ziel verweist (siehe Abbildung 1).
Abbildung 1: Eine globale Aktion, die zu einem verschachtelten Diagramm führt.
Globale Aktion erstellen
So erstellen Sie eine globale Aktion:
Klicken Sie im Grafikeditor auf ein Ziel, um es hervorzuheben.
Klicken Sie mit der rechten Maustaste auf das Ziel, um das Kontextmenü zu öffnen.
Wählen Sie Aktion hinzufügen > Global aus. Links neben dem Ziel wird ein Pfeil () angezeigt.
Klicken Sie auf den Tab Text, um die XML-Textansicht aufzurufen. Das XML für die globale Aktion sieht in etwa so aus:
Wenn Sie eine globale Aktion in Ihrem Code verwenden möchten, übergeben Sie die Ressourcen-ID der globalen Aktion für jedes UI-Element an die Methode navigate(), wie im folgenden Beispiel gezeigt:
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-07-27 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-07-27 (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)"]]