0
0
Android Kotlinmobile~20 mins

StateFlow and SharedFlow in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StateFlow and SharedFlow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference between StateFlow and SharedFlow
Which statement correctly describes the main difference between StateFlow and SharedFlow in Kotlin?
ASharedFlow always holds the latest value and replays it to new collectors, while StateFlow does not hold any value and only emits new values.
BStateFlow always holds the latest value and replays it to new collectors, while SharedFlow does not hold any value by default and can replay a configurable number of values.
CStateFlow and SharedFlow are identical; both always replay all emitted values to new collectors.
DStateFlow is used only for one-time events, while SharedFlow is used for state management.
Attempts:
2 left
💡 Hint
Think about which flow remembers the last value and which one can replay multiple values.
ui_behavior
intermediate
2: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?
AThe collector receives all previous values 1, 2, and 3 in order, then new values.
BThe collector receives only the first emitted value 1 and ignores the rest.
CThe collector receives no values until a new value is emitted after subscription.
DThe collector immediately receives the latest value 3 and then any new values emitted after subscription.
Attempts:
2 left
💡 Hint
Remember what StateFlow holds and replays to new subscribers.
lifecycle
advanced
2: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?
ACollect the SharedFlow in onCreate() using lifecycleScope.launchWhenStarted to ensure events are received only when the Activity is visible.
BCollect the SharedFlow in onDestroy() to catch all events before the Activity is destroyed.
CCollect the SharedFlow in onCreate() without lifecycleScope to avoid missing events during recreation.
DCollect the SharedFlow in onResume() without lifecycle awareness to get all events immediately.
Attempts:
2 left
💡 Hint
Think about lifecycle-aware collection and when UI events should be handled.
📝 Syntax
advanced
1:30remaining
Creating a StateFlow with initial value
Which Kotlin code snippet correctly creates a MutableStateFlow with an initial value of 0?
Aval stateFlow = MutableStateFlow.create(0)
Bval stateFlow = MutableStateFlow<Int>()
Cval stateFlow = MutableStateFlow(0)
Dval stateFlow = MutableStateFlow.value(0)
Attempts:
2 left
💡 Hint
Check the constructor usage for MutableStateFlow.
🔧 Debug
expert
2:30remaining
Why does this SharedFlow not emit to new subscribers?
Consider this SharedFlow declaration:
val sharedFlow = MutableSharedFlow()

New subscribers do not receive any previously emitted values. Why?
ABecause MutableSharedFlow by default has replay = 0, so it does not replay past values to new subscribers.
BBecause MutableSharedFlow requires an initial value to replay to new subscribers.
CBecause SharedFlow only emits values when collected before emission.
DBecause MutableSharedFlow buffers all values and drops new subscribers.
Attempts:
2 left
💡 Hint
Check the default replay parameter of MutableSharedFlow.