Jetpack Compose Academy · Aula

LaunchedEffect para Corrotinas

Inicie trabalho suspenso vinculado à composição.

Aula 1 de 413 etapas

LaunchedEffect para Corrotinas é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎯

Grátis para começar

Aprenda Kotlin com um tutor de IA — grátis

Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.

Cursos
30
Aulas
120

Perguntas Frequentes

A aula “LaunchedEffect para Corrotinas” é grátis?

Sim — o texto completo de “LaunchedEffect para Corrotinas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.

O que vou aprender em “LaunchedEffect para Corrotinas”?

Inicie trabalho suspenso vinculado à composição. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Jetpack Compose Academy?

Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “LaunchedEffect para Corrotinas”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Jetpack Compose Academy?

Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. LaunchedEffect para Corrotinas
  2. rememberCoroutineScope para Eventos
  3. DisposableEffect e Limpeza
  4. derivedStateOf e snapshotFlow
← Voltar para Jetpack Compose Academy