Interview frequency: …
Short answer
LiveData is integrated with the Android lifecycle and works only on the main thread, while StateFlow belongs to Kotlin Coroutines, supports flow operators and working on any dispatcher, and is free of Android dependencies.
Explanation
LiveData is a classic solution tied to the Android SDK, making it unsuitable for domain layers or a Kotlin Multiplatform environment. In contrast, StateFlow always provides an initial value, supports the powerful Flow operators (such as map, filter, combine, debounce), and its data can be collected in Android UIs safely using repeatOnLifecycle.
Code example
// StateFlow cleanly in ViewModel:
class CounterViewModel : ViewModel() {
private val _count = MutableStateFlow(0)
val count: StateFlow<Int> = _count.asStateFlow()
fun increment() { _count.value++ }
}Common mistakes
- Collecting StateFlow data in the UI using plain lifecycleScope.launch without repeatOnLifecycle, which keeps collection active in the background and wastes resources.