StateFlow and SharedFlow help apps handle data that changes over time. They let your app react to updates smoothly and keep the user interface fresh.
StateFlow and SharedFlow in Android Kotlin
val stateFlow = MutableStateFlow(initialValue)
val sharedFlow = MutableSharedFlow<T>(replay = 0)StateFlow always holds the latest value and emits it to new collectors.
SharedFlow can emit events without holding a value, useful for one-time actions.
val countState = MutableStateFlow(0) countState.value = 1
val eventFlow = MutableSharedFlow<String>()
launch { eventFlow.emit("Hello") }val replayFlow = MutableSharedFlow<Int>(replay = 2)This program shows how StateFlow keeps the latest number and SharedFlow sends events. Both are collected and printed.
import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun main() = runBlocking { val stateFlow = MutableStateFlow(0) val sharedFlow = MutableSharedFlow<String>() // Collect StateFlow val job1 = launch { stateFlow.collect { value -> println("StateFlow value: $value") } } // Collect SharedFlow val job2 = launch { sharedFlow.collect { event -> println("SharedFlow event: $event") } } // Update StateFlow stateFlow.value = 1 delay(100) stateFlow.value = 2 // Emit SharedFlow events sharedFlow.emit("First event") sharedFlow.emit("Second event") delay(100) job1.cancel() job2.cancel() }
StateFlow always has a current value; SharedFlow does not have to hold a value.
Use StateFlow for state that changes over time and SharedFlow for events or messages.
Both flows are hot flows that share emissions with multiple collectors.
StateFlow holds and emits the latest state value.
SharedFlow emits events to multiple collectors without holding state.
Use them to build reactive and responsive Android apps easily.
Practice
StateFlow and SharedFlow in Kotlin?Solution
Step 1: Understand
StateFlowbehaviorStateFlowalways holds the latest emitted value and replays it to new collectors.Step 2: Understand
SharedFlowbehaviorSharedFlowemits events but does not hold or replay the latest value by default.Final Answer:
StateFlowholds the latest value,SharedFlowdoes not hold any value. -> Option BQuick Check:
StateFlow holds state, SharedFlow emits events [OK]
- Thinking SharedFlow holds the latest value like StateFlow
- Confusing event emission with state holding
- Assuming both behave the same way
StateFlow with an initial value of 0?Solution
Step 1: Identify correct constructor for StateFlow
MutableStateFlowrequires an initial value and is created withMutableStateFlow(initialValue).Step 2: Check other options
MutableSharedFlowdoes not take an initial value;StateFlowis an interface and cannot be instantiated directly.Final Answer:
val stateFlow = MutableStateFlow(0) -> Option CQuick Check:
MutableStateFlow needs initial value [OK]
- Trying to instantiate StateFlow interface directly
- Using SharedFlow constructor for StateFlow
- Passing initial value to MutableSharedFlow
val stateFlow = MutableStateFlow(10) stateFlow.value = 20 println(stateFlow.value)
Solution
Step 1: Understand MutableStateFlow value assignment
AssigningstateFlow.value = 20updates the current value held by the StateFlow.Step 2: Print the current value
println(stateFlow.value)prints the latest value, which is 20.Final Answer:
20 -> Option DQuick Check:
StateFlow.value reflects latest assigned value [OK]
- Thinking initial value 10 will print instead of updated 20
- Confusing StateFlow with SharedFlow behavior
- Expecting null or error due to reassignment
SharedFlow?
val sharedFlow = MutableSharedFlow<Int>() sharedFlow.value = 5
Solution
Step 1: Check
SharedFlowpropertiesSharedFlowdoes not hold state and has novalueproperty to assign.Step 2: Understand how to emit values
Values are emitted usingemit()ortryEmit()functions, not by assigningvalue.Final Answer:
SharedFlowdoes not have avalueproperty to assign. -> Option AQuick Check:
SharedFlow emits events, no value property [OK]
- Trying to assign value property on SharedFlow
- Confusing SharedFlow with StateFlow
- Assuming SharedFlow holds state like StateFlow
SharedFlow that replays the last 3 emitted values to new collectors. Which configuration is correct?Solution
Step 1: Understand
Thereplayparameter in SharedFlowreplayparameter defines how many past values are replayed to new collectors.Step 2: Check options for replaying last 3 values
Settingreplay = 3inMutableSharedFlowcorrectly configures this behavior.Final Answer:
val sharedFlow = MutableSharedFlow<Int>(replay = 3) -> Option AQuick Check:
Replay count sets how many past events are replayed [OK]
- Confusing bufferSize with replay
- Using StateFlow instead of SharedFlow for replay
- Setting replay to 0 expecting replay behavior
Ready to prove it?
Pass the test and this pattern moves unknown
