Recall & Review
beginner
What is StateFlow in Kotlin?
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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?
✗ 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.