StateFlow is a state-holder observable flow that emits the current and new state updates to its collectors. It always has a current value and is designed to hold and manage UI state in a reactive way.
Click to reveal answer
beginner
How does SharedFlow differ from StateFlow?
SharedFlow is a hot flow that can emit values to multiple collectors but does not hold a current value by default. It is useful for events or broadcasts where you want to share emissions without state retention.
Click to reveal answer
intermediate
What happens when you collect from a StateFlow?
When you collect from a StateFlow, you immediately receive the current value and then any subsequent updates. This makes it perfect for UI components that need the latest state right away.
Click to reveal answer
intermediate
Can SharedFlow replay past emissions to new collectors?
Yes, SharedFlow can be configured with a replay cache size to replay a certain number of past emissions to new collectors. This is useful for sharing recent events with late subscribers.
Click to reveal answer
advanced
Why would you use StateFlow over LiveData in Android?
StateFlow is Kotlin-native, supports coroutines, and offers better control over state updates and emissions. It integrates well with modern Kotlin code and provides a consistent reactive stream for UI state.
Click to reveal answer
Which flow always holds a current value and emits it immediately to new collectors?
AFlow
BStateFlow
CSharedFlow
DChannel
✗ Incorrect
StateFlow always holds a current value and emits it immediately to new collectors.
What is a key feature of SharedFlow that StateFlow does not have by default?
AIs cold and lazy
BHolds a current value
CCan replay past emissions
DSupports only one collector
✗ Incorrect
SharedFlow can be configured to replay past emissions to new collectors, unlike StateFlow which always holds only the latest value.
Which of these is best suited for representing UI state that changes over time?
AStateFlow
BSharedFlow
CChannel
DBroadcastReceiver
✗ Incorrect
StateFlow is designed to hold and emit UI state changes over time.
If you want to send one-time events like navigation commands, which flow is more appropriate?
ASharedFlow
BStateFlow
CLiveData
DFlow
✗ Incorrect
SharedFlow is better for one-time events because it does not hold a state but can emit events to multiple collectors.
How do you update the value inside a StateFlow?
ABy calling emit()
BBy subscribing to it
CBy calling collect()
DBy setting its value property
✗ Incorrect
StateFlow's value can be updated by setting its value property directly.
Explain the main differences between StateFlow and SharedFlow and when to use each.
Think about holding state versus broadcasting events.
You got /4 concepts.
Describe how you would use StateFlow in an Android app to manage UI state reactively.
Consider ViewModel and UI communication.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between StateFlow and SharedFlow in Kotlin?
easy
A. Both StateFlow and SharedFlow hold the latest value.
B. StateFlow holds the latest value, SharedFlow does not hold any value.
C. SharedFlow holds the latest value, StateFlow does not hold any value.
D. Neither StateFlow nor SharedFlow hold any value.
Solution
Step 1: Understand StateFlow behavior
StateFlow always holds the latest emitted value and replays it to new collectors.
Step 2: Understand SharedFlow behavior
SharedFlow emits events but does not hold or replay the latest value by default.
Final Answer:
StateFlow holds the latest value, SharedFlow does not hold any value. -> Option B