Which side-effect handler for a one-time coroutine on composition entry?

Tests Compose side-effect API boundaries. Answer: LaunchedEffect(Unit); it runs once and auto-cancels on exit. Contrast with rememberCoroutineScope for user-triggered callbacks. Red flag: picking SideEffect or rememberCoroutineScope and missing cancellation.
What's really being asked
Your ability to select the correct Compose side-effect API based on lifecycle and execution model. The interviewer wants to see that you understand the difference between coroutine-based effects tied to composition, synchronous recompositions, and user-triggered scope launching. Specifically they are checking if you know which API auto-manages cancellation and scope creation.
The full answer
First, name LaunchedEffect and pass a constant key such as Unit or true so the block executes exactly once when the composable enters composition. Second, explain that LaunchedEffect creates a coroutine scoped to the composition which is automatically cancelled if the composable leaves the composition, preventing leaks and stale work. Third, contrast this with rememberCoroutineScope, which is intended for event handlers and user-triggered actions like button clicks where you need a scope in a callback; it does not auto-run on entry and requires manual lifecycle awareness. Fourth, mention SideEffect only to exclude it, noting it runs synchronously on every successful recomposition and is not a coroutine builder. Fifth, optionally note DisposableEffect is for setup and teardown of resources like listeners, not for one-shot async fetches.
The mistakes people make
Picking rememberCoroutineScope and wrapping the call in a remembered flag or LaunchedEffect-like logic shows you are forcing an API meant for callbacks into a lifecycle role. Using SideEffect demonstrates confusion between synchronous recompositions and asynchronous coroutine work. Forgetting the key parameter or using a changing key like a state object means the effect restarts on every update, violating the one-time requirement. Launching a bare GlobalScope or lifecycleScope inside a composable without tying it to composition exit is a serious red flag for leaks.
What usually comes next
How would you handle the fetch being restarted if the user rotates the screen? The follow-up tests whether you mention ViewModel, rememberSaveable, or configuration change handling outside of LaunchedEffect. What if you need to cancel and restart when a parameter changes? Then you would use LaunchedEffect(key) with the parameter as the key. When would you use rememberCoroutineScope instead? When a user clicks a button and you need to launch a snackbar or animation from a callback outside of the composition flow.
A concrete example
You have a ProfileScreen composable that needs to load user data from a network endpoint when the screen appears. Inside ProfileScreen you write LaunchedEffect(Unit) { viewModel.loadProfile() }. Because Unit never changes, this triggers once on entry. If the user navigates away before the request completes, the coroutine is cancelled automatically. You do not write val scope = rememberCoroutineScope() followed by scope.launch { ... } at the top level of the composable because that would fire immediately during composition without lifecycle-aware cancellation semantics and would be the wrong pattern for an automatic entry effect.
Interview question
You need to load data in a coroutine exactly once when a composable enters composition and cancel automatically if it exits. Which approach is correct?
- a.LaunchedEffect with a mutable state object as its key
- b.rememberCoroutineScope with its launch called immediately in the composable body
- c.SideEffect containing a coroutine launch block
- d.LaunchedEffect with a constant key such as UnitCorrect
Why? this is the answer
LaunchedEffect(Unit) creates a composition-scoped coroutine that runs once on entry and cancels automatically when the composable leaves. rememberCoroutineScope is intended for event handlers like button clicks and lacks automatic lifecycle-aware cancellation when launched directly during composition.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #jetpack-compose
- #side-effects
- #coroutines
- #intermediate
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles