Utiliser les actions de navigation et les fragments
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Vous pouvez établir des connexions entre fragments à l'aide des actions de navigation. Lorsque vous appelez une action de navigation, l'utilisateur passe d'une destination à une autre. Ce guide explique en quoi consistent les actions, comment les créer et les utiliser.
Présentation
Chaque action possède un identifiant unique et peut contenir des attributs supplémentaires, tels qu'une destination. La destination définit l'écran sur lequel l'application redirige l'utilisateur lorsqu'il déclenche l'action. L'action peut également utiliser des arguments pour transférer des données d'une destination à une autre.
Safe Args : à l'aide d'actions, vous pouvez remplacer les ID de ressource par des opérations générées par Safe Args, ce qui offre une sécurité supplémentaire au moment de la compilation.
Définissez des actions dans le fichier XML de votre graphique de navigation à l'aide des balises <action>. L'extrait de code suivant implémente une action qui représente une transition de FragmentA vers FragmentB.
Vous pouvez utiliser des actions générales pour accéder à une destination depuis n'importe quel emplacement.
Pour toute destination au sein de votre appli accessible via plusieurs chemins, définissez une action globale correspondante permettant d'accéder à cette destination.
Prenons l'exemple suivant : les destinations results_winner et game_over doivent toutes deux apparaître dans la destination d'accueil. L'action action_pop_out_of_game étant une action globale en dehors action_pop_out_of_gamed'un fragment spécifique, elle permet de le faire.
Cela signifie que vous pouvez la référencer et l'appeler n'importe où au sein de in_game_nav_graph.
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/29 (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/29 (UTC)."],[],[],null,["# Use Navigation actions and Fragments\n\nYou can build connections between fragments using navigation actions. Evoking a\nnavigation action takes the user from one destination to another. This guide\nexplains what actions are and demonstrates how you can create and use them.\n| **Warning:** The navigation actions API is available only when using the views UI framework.\n\nOverview\n--------\n\nEach action has a unique ID and can contain additional attributes, such as a\ndestination. The destination defines the screen to which the app takes the user\nwhen they trigger the action. The action can also use arguments to carry data\nfrom one destination to another.\n\n- **Safe Args:** Using actions, you can replace resource IDs with [Safe\n Args-generated operations](/guide/navigation/design/actions), providing additional compile-time safety.\n- **Animations:** You can also animate transitions between the destinations. For more information, see [Animate transitions between destinations](/reference/androidx/navigation/Navigation#createNavigateOnClickListener(int)).\n\nExamples\n--------\n\nDefine actions in your navigation graph XML file using the `\u003caction\u003e` tags. The\nfollowing snippet implements an action that represents a transition from\n`FragmentA` to `FragmentB`. \n\n \u003cfragment\n android:id=\"@+id/fragmentA\"\n android:name=\"com.example.FragmentA\"\u003e\n \u003caction\n android:id=\"@+id/action_fragmentA_to_fragmentB\"\n app:destination=\"@id/fragmentB\" /\u003e\n \u003c/fragment\u003e\n\n### Navigate using an action\n\nTo navigate using this action, you call [`NavController.navigate()`](/reference/androidx/navigation/NavController#navigate) and pass\nit the action's `id`: \n\n navController.navigate(R.id.action_fragmentA_to_fragmentB)\n\n| **Important:** For more information about how to use `NavController.navigate()` and its various overloads, see the [Navigate to a\n| destination](/guide/navigation/use-graph/navigate) guide.\n\nGlobal actions\n--------------\n\nYou can use global actions to navigate to a destination from anywhere.\n\nFor any destination in your app that is accessible through more than one path,\ndefine a corresponding global action that navigates to that\ndestination.\n\nConsider the following example. The `results_winner` and `game_over`\ndestinations both need to pop up to the home destination. The\n`action_pop_out_of_game` action provides the ability to do so;\n`action_pop_out_of_game`is a global action outside of any specific fragment.\nThat means you can reference and call it anywhere within the\n`in_game_nav_graph`. \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cnavigation xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:id=\"@+id/in_game_nav_graph\"\n app:startDestination=\"@id/in_game\"\u003e\n\n \u003c!-- Action back to destination which launched into this in_game_nav_graph --\u003e\n \u003caction android:id=\"@+id/action_pop_out_of_game\"\n app:popUpTo=\"@id/in_game_nav_graph\"\n app:popUpToInclusive=\"true\" /\u003e\n\n \u003cfragment\n android:id=\"@+id/in_game\"\n android:name=\"com.example.android.gamemodule.InGame\"\n android:label=\"Game\"\u003e\n \u003caction\n android:id=\"@+id/action_in_game_to_resultsWinner\"\n app:destination=\"@id/results_winner\" /\u003e\n \u003caction\n android:id=\"@+id/action_in_game_to_gameOver\"\n app:destination=\"@id/game_over\" /\u003e\n \u003c/fragment\u003e\n\n \u003cfragment\n android:id=\"@+id/results_winner\"\n android:name=\"com.example.android.gamemodule.ResultsWinner\" /\u003e\n\n \u003cfragment\n android:id=\"@+id/game_over\"\n android:name=\"com.example.android.gamemodule.GameOver\"\n android:label=\"fragment_game_over\"\n tools:layout=\"@layout/fragment_game_over\" /\u003e\n\n \u003c/navigation\u003e"]]