LaunchedEffect untuk Coroutine
Mulai pekerjaan suspend yang terikat pada komposisi.
LaunchedEffect untuk Coroutine adalah pelajaran Jetpack Compose Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Jetpack Compose Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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. 🎯
Pertanyaan yang Sering Diajukan
Apakah pelajaran “LaunchedEffect untuk Coroutine” gratis?
Ya — teks lengkap “LaunchedEffect untuk Coroutine” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Jetpack Compose Academy, upgrade ke CoddyKit PRO. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “LaunchedEffect untuk Coroutine”?
Mulai pekerjaan suspend yang terikat pada komposisi. Kamu berlatih Jetpack Compose Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Jetpack Compose Academy?
Tidak diperlukan pengalaman sebelumnya. Jetpack Compose Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.
Berapa lama pelajaran “LaunchedEffect untuk Coroutine” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Jetpack Compose Academy ini?
Ya. Setiap pelajaran Jetpack Compose Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- LaunchedEffect untuk Coroutine
- rememberCoroutineScope untuk Event
- DisposableEffect & Pembersihan
- derivedStateOf & snapshotFlow