What is the difference between viewModelScope and lifecycleScope?

This tests scope ownership across configuration changes. viewModelScope survives rotation because it lives in ViewModel, while lifecycleScope dies with UI; use former for logic and latter for UI tasks. Never launch data loads in lifecycleScope.
WHAT THIS TESTS: This question evaluates whether you understand the ownership model of structured concurrency in the Android UI layer and can distinguish between a scope that survives configuration changes and one that is tightly coupled to a UI component's lifecycle. Interviewers want to see that you know where to launch coroutines so they do not leak, duplicate, or cancel at the wrong time. They also want to hear that you respect the single source of truth principle by keeping long-running data production out of UI-bound scopes.
A GOOD ANSWER COVERS: First, define viewModelScope as a CoroutineScope tied to a ViewModel that lives until onCleared is called, meaning it survives configuration changes like screen rotation. Second, define lifecycleScope as a CoroutineScope tied to a LifecycleOwner such as an Activity or Fragment that cancels when the lifecycle reaches DESTROYED. Third, explain the decision rule: use viewModelScope for business logic, data transformations, and repository calls that should continue across rotation; use lifecycleScope for UI-scoped work like animations, scrolling telemetry, or one-shot events that should not outlive the current window. Fourth, mention that lifecycleScope is safe for collecting flows in the UI when paired with repeatOnLifecycle to avoid wasting resources in the background. Finally, note that viewModelScope uses a SupervisorJob so child failures do not crash unrelated work.
COMMON WRONG ANSWERS: A red flag is claiming the two scopes are interchangeable. Another mistake is saying viewModelScope is global or lives for the entire application; it is local to a single ViewModel instance. Candidates sometimes argue that lifecycleScope survives rotation because it is lifecycle-aware, which is backwards. Also, using lifecycleScope to trigger a network request and then wondering why the result never arrives after rotation is a classic anti-pattern that signals weak architecture intuition.
LIKELY FOLLOW-UPS: The interviewer may ask how to collect a StateFlow in a Fragment without leaking the collector, which leads to viewLifecycleOwner.lifecycleScope and repeatOnLifecycle. They might ask what happens if you launch a coroutine in a Fragment's lifecycleScope during onCreateView and the user rotates the screen. They could also ask about custom scopes, such as when to use a manually managed SupervisorJob versus these built-in options.
ONE CONCRETE EXAMPLE: Imagine a search screen with a ViewModel that debounces user input and queries a repository. You launch the debounce collector in viewModelScope so that typing continues smoothly across rotation. However, you use lifecycleScope to trigger a one-time snackbar message when the Fragment receives a one-shot event from the ViewModel via Channel or SharedFlow, because the snackbar should only show while the Fragment is visible and should disappear if the user leaves the screen.
Source: developer.android.com
Read the original → developer.android.com
- #android
- #kotlin
- #coroutines
- #viewmodel
- #lifecycle
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.