0
0
Android Kotlinmobile~5 mins

StateFlow and SharedFlow in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AFlow
BStateFlow
CSharedFlow
DChannel
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
Which of these is best suited for representing UI state that changes over time?
AStateFlow
BSharedFlow
CChannel
DBroadcastReceiver
If you want to send one-time events like navigation commands, which flow is more appropriate?
ASharedFlow
BStateFlow
CLiveData
DFlow
How do you update the value inside a StateFlow?
ABy calling emit()
BBy subscribing to it
CBy calling collect()
DBy setting its value property
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.