Challenge - 5 Problems
StateFlow and SharedFlow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Difference between StateFlow and SharedFlow
Which statement correctly describes the main difference between StateFlow and SharedFlow in Kotlin?
Attempts:
2 left
💡 Hint
Think about which flow remembers the last value and which one can replay multiple values.
✗ Incorrect
StateFlow always holds the latest value and emits it immediately to new collectors. SharedFlow does not hold a value by default but can be configured to replay a set number of past values.
❓ ui_behavior
intermediate2:00remaining
StateFlow UI update behavior
Given a StateFlow that emits numbers, what will happen in the UI if a collector subscribes after the StateFlow has emitted values 1, 2, and 3?
Attempts:
2 left
💡 Hint
Remember what StateFlow holds and replays to new subscribers.
✗ Incorrect
StateFlow always holds the latest value and emits it immediately to new collectors. It does not replay all past values, only the latest one.
❓ lifecycle
advanced2:30remaining
SharedFlow and lifecycle-aware collection
You have a SharedFlow that emits UI events like navigation commands. What is the best way to collect this SharedFlow in an Android Activity to avoid missing events when the Activity is recreated?
Attempts:
2 left
💡 Hint
Think about lifecycle-aware collection and when UI events should be handled.
✗ Incorrect
Using lifecycleScope.launchWhenStarted ensures the SharedFlow is collected only when the Activity is visible, preventing missed events and avoiding leaks.
📝 Syntax
advanced1:30remaining
Creating a StateFlow with initial value
Which Kotlin code snippet correctly creates a MutableStateFlow with an initial value of 0?
Attempts:
2 left
💡 Hint
Check the constructor usage for MutableStateFlow.
✗ Incorrect
MutableStateFlow requires an initial value passed directly to its constructor.
🔧 Debug
expert2:30remaining
Why does this SharedFlow not emit to new subscribers?
Consider this SharedFlow declaration:
New subscribers do not receive any previously emitted values. Why?
val sharedFlow = MutableSharedFlow()
New subscribers do not receive any previously emitted values. Why?
Attempts:
2 left
💡 Hint
Check the default replay parameter of MutableSharedFlow.
✗ Incorrect
MutableSharedFlow by default has replay set to 0, so it does not replay any past values to new subscribers.