Recall & Review
beginner
What is SharedFlow in Kotlin?
SharedFlow is a hot flow that allows multiple collectors to receive the same emitted values. It is useful for sharing events or data streams across different parts of an app.
Click to reveal answer
intermediate
Why use SharedFlow for events instead of
StateFlow?SharedFlow is better for one-time events because it can replay a configurable number of past events and does not hold a current state like StateFlow. StateFlow always holds the latest value, which is not ideal for events.
Click to reveal answer
beginner
How do you configure a SharedFlow to replay the last event for new subscribers?
You set the
replay parameter when creating the SharedFlow. For example, MutableSharedFlow(replay = 1) will replay the last emitted event to new subscribers.Click to reveal answer
beginner
What does it mean that SharedFlow is a hot flow?
A hot flow means it emits values regardless of whether there are active collectors. Events are produced continuously and shared with all subscribers.
Click to reveal answer
beginner
How can you emit an event using a MutableSharedFlow?
You call the
emit() suspend function or tryEmit() function on the MutableSharedFlow instance to send an event to all collectors.Click to reveal answer
What parameter controls how many past events a SharedFlow replays to new subscribers?
✗ Incorrect
The 'replay' parameter defines how many past events are replayed to new subscribers.
Which of these is true about SharedFlow?
✗ Incorrect
SharedFlow is a hot flow, meaning it emits events regardless of active collectors.
Which function is used to send an event to a MutableSharedFlow?
✗ Incorrect
The emit() suspend function is used to send events to a MutableSharedFlow.
Why is SharedFlow preferred over StateFlow for one-time events?
✗ Incorrect
SharedFlow can replay a limited number of events and does not hold a current state, making it better for one-time events.
What happens if you collect a SharedFlow after some events have already been emitted and replay is set to 0?
✗ Incorrect
With replay set to 0, new collectors receive only new events emitted after they start collecting.
Explain how SharedFlow works for event handling in Android Kotlin apps.
Think about how events are shared and replayed to subscribers.
You got /5 concepts.
Describe the difference between SharedFlow and StateFlow and when to use each.
Consider how each flow handles data and events.
You got /5 concepts.