Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Puoi creare connessioni tra frammenti utilizzando le azioni di navigazione. L'evocazione di un
l'azione di navigazione porta l'utente da una destinazione a un'altra. Questa guida
spiega cosa sono le azioni e come è possibile crearle e utilizzarle.
Panoramica
Ogni azione ha un ID univoco e può contenere attributi aggiuntivi, come
destinazione. La destinazione definisce la schermata a cui l'app indirizza l'utente
quando attivano l'azione. L'azione può anche utilizzare argomenti per trasportare i dati
da una destinazione a un'altra.
Args sicuri: utilizzando le azioni, puoi sostituire gli ID risorsa con Sicuro
Operazioni generate da Args, che forniscono ulteriore sicurezza in fase di compilazione.
Definisci le azioni nel file XML del grafico di navigazione utilizzando i tag <action>. La
il seguente snippet implementa un'azione che rappresenta una transizione
Da FragmentA a FragmentB.
Puoi utilizzare azioni globali per raggiungere una destinazione ovunque ti trovi.
Per qualsiasi destinazione dell'app a cui è possibile accedere tramite più di un percorso,
definisci un'azione globale corrispondente che conduce a quell'azione
destinazione.
Considera il seguente esempio. results_winner e game_over
entrambe le destinazioni devono apparire
nella destinazione di casa. La
L'azione action_pop_out_of_game consente di farlo;
action_pop_out_of_gameè un'azione globale esterna a uno specifico frammento.
Ciò significa che puoi farvi riferimento e chiamarlo ovunque all'interno
in_game_nav_graph.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-29 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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"]]