Pasar argumentos entre pantallas
Envíe datos mediante la ruta de navegación.
Pasar argumentos entre pantallas es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Jetpack Compose Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Screens Need Data
A detail screen needs to know which item to show. Navigation Compose lets you carry that data along the route itself.
Declare a Placeholder
Add a placeholder in curly braces to a route. It marks where a value will slot in when you navigate.
composable("profile/{userId}") { backStackEntry ->
ProfileScreen()
}Define the Argument
List each placeholder in arguments and give it a type. This tells Compose how to parse the value out of the route.
composable(
"profile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.IntType })
) { }Send the Value
To pass data, build the route string with the real value in place of the placeholder. The receiving screen reads it back out.
navController.navigate("profile/42")Read It from the Entry
The backStackEntry carries the arguments bundle. Pull your value out by the same name you declared in the route.
composable("profile/{userId}") { entry ->
val id = entry.arguments?.getInt("userId")
ProfileScreen(userId = id)
}Choose the Right NavType
The NavType decides parsing: StringType, IntType, BoolType, and more. Match it to your data so values arrive correctly typed.
Optional Arguments
Use a query-style optional argument with a default value for data that may be missing, like a filter that is not always set.
composable(
"search?query={query}",
arguments = listOf(navArgument("query") { defaultValue = "" })
) { }Pass Small Data Only
Route arguments are for identifiers, not whole objects. Send an id, then load the full data from your repository or ViewModel.
Encode Tricky Values
Strings with slashes or spaces can break a route. URL-encode them before navigating and decode after reading, just like a real URL.
Let the ViewModel Read It
A ViewModel can grab arguments from SavedStateHandle, so your screen stays clean and survives recreation with the right data.
class ProfileViewModel(state: SavedStateHandle) {
val userId: Int = checkNotNull(state["userId"])
}Keep Argument Names Aligned
The placeholder, the navArgument, and the lookup key must all use the same name. A mismatch returns null instead of your value.
Quick Check
How should you send a complex item to a detail screen?
Recap
You can now pass data by adding a placeholder to a route, typing it with navArgument, and reading it back from the back stack entry. 📦
Aprende Kotlin con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 30
- Lecciones
- 120
Preguntas frecuentes
¿La lección «Pasar argumentos entre pantallas» es gratis?
Sí — el texto completo de «Pasar argumentos entre pantallas» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Jetpack Compose Academy, actualiza a CoddyKit PRO. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Pasar argumentos entre pantallas»?
Envíe datos mediante la ruta de navegación. Practicas Jetpack Compose Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Jetpack Compose Academy?
No se requiere experiencia previa. Jetpack Compose Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Pasar argumentos entre pantallas»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Jetpack Compose Academy?
Sí. Cada lección de Jetpack Compose Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- NavController y NavHost
- Destinos y rutas Composable
- Pasar argumentos entre pantallas
- Pila de navegación y deep links