Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Vous pouvez utiliser une action globale pour créer une action commune que plusieurs destinations peuvent utiliser. Imaginons que vous souhaitiez que les boutons de différentes destinations renvoient vers le même écran d'application principal.
Une action globale est représentée dans l'éditeur de navigation par une petite flèche pointant vers la destination associée, comme illustré dans la figure 1.
Figure 1. A global action that leads to a nested
graph.
Créer une action globale
Pour créer une action globale, procédez comme suit :
Dans l'éditeur de graphique, cliquez sur une destination pour la sélectionner.
Effectuez un clic droit pour afficher le menu contextuel.
Sélectionnez Add Action > Global (Ajouter une action > Générale). Une flèche () apparaît à gauche de la destination.
Cliquez sur l'onglet Text (Texte) pour accéder à la vue XML. Le code XML de l'action globale se présente comme suit :
Pour utiliser une action globale dans votre code, transmettez son ID de ressource à la méthode navigate() pour chaque élément d'interface utilisateur, comme illustré dans l'exemple suivant :
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)"]]