androidengineers.Book a session
โ† All interview questions
Jetpack ComposeIntermediate2 min

A lesson screen keeps showing the first lesson after its ID changes. How would you debug the effect?

Answer

Check whether loading runs inside LaunchedEffect(Unit). That effect does not restart merely because a captured lesson ID changes while the same call remains in composition.

For composition-owned suspending work, key the effect to the resource identity:

LaunchedEffect(lessonId) {
    // loadLesson suspends here and cooperates with cancellation.
    loadLesson(lessonId)
}

Changing the key cancels the previous effect coroutine and launches another. If loadLesson launches an unrelated job and returns immediately, cancellation of the effect does not automatically cancel that independent job. Trace the actual ownership of the request.

Design trade-off

For screen data, a ViewModel can own the selected ID and loading pipeline instead. Avoid starting the same request from both a ViewModel initializer and a UI effect. Define one owner, a loading state, and a stale-response policy.

Follow-up to practise

What if only a completion callback changes? rememberUpdatedState can let ongoing work use a current callback without restarting it. Do not use that technique to hide a resource ID that genuinely should restart the work.

Reference

Android Developers: Side-effects in Compose

Mark this when you can explain the answer in your own words.

Share & Help Others

Help fellow developers prepare for interviews

Sharing helps the Android community grow ๐Ÿ’š

Keep practising