Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Puedes crear conexiones entre fragmentos con acciones de Navigation. Cuando se invoca una acción de Navigation, el usuario pasa de un destino a otro. En esta guía, se explica qué son las acciones, y se muestra cómo puedes crearlas y usarlas.
Descripción general
Cada acción tiene un ID único y puede contener atributos adicionales, como un destino. El destino define la pantalla a la que la app dirige al usuario cuando inicia la acción. La acción también puede usar argumentos para transportar datos de un destino a otro.
Safe Args: Con acciones, puedes reemplazar los IDs de recursos por operaciones generadas con Safe Args, lo que proporciona seguridad adicional en el tiempo de compilación.
Animaciones: También puedes animar las transiciones entre los destinos.
Si quieres obtener más información, consulta Cómo animar transiciones entre destinos.
Ejemplos
Define acciones en el archivo en formato XML de tu gráfico de navegación con las etiquetas <action>. En el siguiente fragmento, se implementa una acción que representa una transición de FragmentA a FragmentB.
Puedes usar acciones globales para navegar a un destino desde cualquier lugar.
Para cualquier destino de tu app al que se pueda acceder a través de más de una ruta, define una acción global correspondiente que navegue a ese destino.
Ten en cuenta el siguiente ejemplo. Los destinos results_winner y game_over deben aparecer en el destino de inicio. La acción action_pop_out_of_game brinda la capacidad de hacerlo; action_pop_out_of_game es una acción global fuera de cualquier fragmento específico.
Es decir, puedes hacer referencia a este y llamarlo desde cualquier lugar dentro de in_game_nav_graph.
El contenido y las muestras de código que aparecen en esta página están sujetas a las licencias que se describen en la Licencia de Contenido. Java y OpenJDK son marcas registradas de Oracle o sus afiliados.
Última actualización: 2025-07-29 (UTC)
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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"]]