LaunchedEffect para coroutines
Inicie trabajo suspendido vinculado a la composición.
LaunchedEffect para coroutines es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 1 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.
Effects Live Outside Composition
Composables should be pure and run many times. When you need to do real work like a network call, you reach for a side effect handler instead.
Meet LaunchedEffect
A LaunchedEffect runs suspend code in a coroutine that is tied to where it sits in your composition. Perfect for one-shot startup work. 🚀
It Starts When It Enters
When a LaunchedEffect first enters the composition, it launches its block. When it leaves the composition, the coroutine is cancelled for you.
LaunchedEffect(Unit) {
loadProfile()
}The Key Controls Restarts
The first argument is a key. If the key changes between recompositions, the old coroutine is cancelled and a fresh one starts.
LaunchedEffect(userId) {
user = fetchUser(userId)
}Use Unit to Run Once
Passing Unit (or true) as the key means the effect never restarts. It runs a single time when the Composable appears.
LaunchedEffect(Unit) {
analytics.logScreenView()
}Suspend Calls Are Welcome
Inside the block you can freely call suspend functions like delay or a Retrofit request. The coroutine scope is provided for you.
LaunchedEffect(Unit) {
delay(2000)
showSplash = false
}Never Launch in the Body
Calling a coroutine directly in the Composable body is a bug. The body re-runs on every recomposition, so you would fire the work again and again.
Cancellation Is Automatic
When the user navigates away and the Composable leaves the screen, Compose cancels the coroutine. No manual cleanup, no leaks.
Pair It with State
Effects usually write their result into state so the UI redraws. Store the loaded data, and recomposition shows it.
var data by remember { mutableStateOf<List<Item>>(emptyList()) }
LaunchedEffect(Unit) { data = repo.load() }Multiple Keys for Combined Triggers
You can pass several keys. The effect restarts whenever any of them changes, which is handy for re-fetching on filter changes.
LaunchedEffect(query, sort) {
results = search(query, sort)
}A Typical Loading Pattern
Together it reads like a recipe: set loading true, await the result, then store it. Compose handles the lifecycle around your coroutine.
LaunchedEffect(id) {
loading = true
item = repo.get(id)
loading = false
}Quick Check
Think about how restarts are controlled.
Recap: LaunchedEffect
Nice work! LaunchedEffect runs suspend code tied to composition, restarts when its key changes, and cancels itself automatically when the Composable leaves. 🎯
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 «LaunchedEffect para coroutines» es gratis?
Sí — el texto completo de «LaunchedEffect para coroutines» 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 «LaunchedEffect para coroutines»?
Inicie trabajo suspendido vinculado a la composició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 1 de 4.
¿Cuánto tiempo toma la lección «LaunchedEffect para coroutines»?
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
- LaunchedEffect para coroutines
- rememberCoroutineScope para eventos
- DisposableEffect y limpieza
- derivedStateOf y snapshotFlow