A side effect is an operation that affects something outside the scope of a composable function, like launching a coroutine, updating a variable, or interacting with the system.
Click to reveal answer
beginner
What does LaunchedEffect do in Jetpack Compose?
LaunchedEffect runs a suspend function when the key(s) change or when the composable enters the composition. It is used to launch coroutines tied to the composable lifecycle.
Click to reveal answer
intermediate
How does SideEffect differ from LaunchedEffect?
SideEffect runs synchronously after every successful recomposition to perform non-suspending side effects, while LaunchedEffect launches coroutines asynchronously.
Click to reveal answer
beginner
When should you use LaunchedEffect in your composable?
Use LaunchedEffect when you want to run suspend functions or launch coroutines that depend on some state or key and should be cancelled and restarted when that key changes.
Click to reveal answer
intermediate
Give an example of a side effect that SideEffect is suitable for.
SideEffect is suitable for updating external objects like logging, analytics, or updating a mutable state outside Compose after recomposition.
Click to reveal answer
What happens when the key passed to LaunchedEffect changes?
A. Prints "Count: 0" repeatedly on every recomposition
B. Prints "Count: 1" repeatedly on every recomposition
C. Prints "Count: 1" once after first composition
D. Prints "Count: 0" once and never again
Solution
Step 1: Analyze LaunchedEffect behavior
LaunchedEffect(Unit) runs once on first composition and increments count to 1.
Step 2: Analyze SideEffect behavior
SideEffect runs after every recomposition and prints current count value. However, since count is a regular variable and not a state, recompositions do not trigger updates to count or cause recompositions to reflect the incremented value. The printed value remains 0 repeatedly.
Final Answer:
Prints "Count: 0" repeatedly on every recomposition -> Option A
Quick Check:
Regular variable not triggering recomposition; SideEffect prints initial value repeatedly = B [OK]
Hint: Regular variables don't trigger recomposition; SideEffect prints initial value repeatedly [OK]
Common Mistakes:
Assuming SideEffect runs only once
Thinking count increments on every recomposition
Confusing LaunchedEffect and SideEffect timing
4. Identify the error in this code using LaunchedEffect:
@Composable
fun MyScreen() {
LaunchedEffect {
delay(1000)
println("Hello")
}
}
medium
A. Cannot call delay inside LaunchedEffect
B. LaunchedEffect must be called outside composable
C. println cannot be used inside composable
D. Missing key parameter in LaunchedEffect call
Solution
Step 1: Check LaunchedEffect syntax
LaunchedEffect requires at least one key parameter to track changes and restart the coroutine.
Step 2: Confirm other parts are valid
Calling delay and println inside LaunchedEffect is allowed; it runs suspend code.
Final Answer:
Missing key parameter in LaunchedEffect call -> Option D
Quick Check:
LaunchedEffect needs key parameter = B [OK]
Hint: LaunchedEffect always needs a key parameter [OK]
Common Mistakes:
Omitting key parameter in LaunchedEffect
Thinking delay is disallowed inside LaunchedEffect
Believing println can't be used in composables
5. You want to fetch data from the network only once when a screen appears and log a message after every UI update. Which combination correctly uses LaunchedEffect and SideEffect?
hard
A. Use LaunchedEffect without key for fetching and SideEffect(Unit) for logging
B. Use SideEffect(Unit) for fetching and LaunchedEffect { println("UI updated") } for logging
C. Use LaunchedEffect(Unit) for fetching and SideEffect { println("UI updated") } for logging
D. Use SideEffect with key for fetching and LaunchedEffect(Unit) for logging
Solution
Step 1: Identify correct usage for fetching once
LaunchedEffect with Unit key runs once on first composition, ideal for fetching data.
Step 2: Identify correct usage for logging after UI updates
SideEffect runs after every recomposition, perfect for logging UI updates.
Final Answer:
Use LaunchedEffect(Unit) for fetching and SideEffect { println("UI updated") } for logging -> Option C
Quick Check:
LaunchedEffect for one-time suspend; SideEffect for every UI update = A [OK]
Hint: LaunchedEffect for one-time tasks; SideEffect for every UI update [OK]
Common Mistakes:
Using SideEffect for suspend fetching
Omitting key in LaunchedEffect
Confusing which runs once vs every recomposition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.