SharedFlow is a hot flow that allows multiple subscribers to receive the same emitted values, useful for broadcasting events to many collectors.
Click to reveal answer
intermediate
How does SharedFlow differ from StateFlow?
SharedFlow does not hold a current value and is used for events, while StateFlow always holds the latest value and is used for state updates.
Click to reveal answer
intermediate
What does the replay parameter in SharedFlow control?
The replay parameter controls how many past emitted values are buffered and replayed to new subscribers when they start collecting.
Click to reveal answer
beginner
Why is SharedFlow considered a hot flow?
Because it emits values regardless of whether there are active subscribers, unlike cold flows which start emitting only when collected.
Click to reveal answer
beginner
How do you emit an event to a SharedFlow?
You use the emit() or tryEmit() functions on the MutableSharedFlow instance to send events to all subscribers.
Click to reveal answer
What type of flow is SharedFlow in Kotlin?
ACold flow
BSingle emission flow
CState holder
DHot flow
✗ Incorrect
SharedFlow is a hot flow that emits values regardless of active subscribers.
Which parameter controls how many past events SharedFlow replays to new subscribers?
Acapacity
BbufferSize
Creplay
Dtimeout
✗ Incorrect
The replay parameter sets how many past events are replayed to new subscribers.
Which function is used to send an event to a SharedFlow?
Aemit()
Blaunch()
Csubscribe()
Dcollect()
✗ Incorrect
emit() is used to send events to a SharedFlow.
What happens if no subscribers are collecting a SharedFlow when an event is emitted?
AEvent is lost
BEvent is buffered if replay > 0
CFlow stops
DException is thrown
✗ Incorrect
Events are buffered up to the replay count and delivered to future subscribers.
Which of these is NOT a typical use case for SharedFlow?
AHolding the latest state value
BBroadcasting UI events
CSending one-time notifications
DSharing events between multiple collectors
✗ Incorrect
Holding the latest state value is the role of StateFlow, not SharedFlow.
Explain how SharedFlow helps in broadcasting events to multiple subscribers in Kotlin.
Think about how events are sent and received by many listeners at once.
You got /4 concepts.
Describe the difference between SharedFlow and StateFlow and when you would use each.
Consider if you want to hold a value or just send events.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of SharedFlow in Kotlin?
easy
A. To broadcast events to multiple listeners simultaneously
B. To store data permanently in a database
C. To perform heavy computations asynchronously
D. To create a single-threaded event queue
Solution
Step 1: Understand SharedFlow's role
SharedFlow is designed to send events to many listeners at once, like a radio broadcast.
Step 2: Compare with other options
It does not store data permanently, perform heavy computations, or create single-threaded queues.
Final Answer:
To broadcast events to multiple listeners simultaneously -> Option A
Quick Check:
SharedFlow = Broadcast events [OK]
Hint: SharedFlow is like a radio sending to many listeners [OK]
Common Mistakes:
Confusing SharedFlow with data storage
Thinking SharedFlow handles computations
Assuming SharedFlow is single-threaded queue
2. Which of the following is the correct way to create a SharedFlow with replay of 2 events?
easy
A. val flow = SharedFlow<Int>(replayCount = 2)
B. val flow = MutableSharedFlow<Int>(replay = 2)
C. val flow = MutableSharedFlow<Int>(buffer = 2)
D. val flow = SharedFlow<Int>(bufferSize = 2)
Solution
Step 1: Recall SharedFlow creation syntax
MutableSharedFlow constructor uses parameter replay to set how many past events to keep.
Step 2: Check options for correct parameter
Only val flow = MutableSharedFlow<Int>(replay = 2) uses replay = 2 correctly. Others use wrong parameter names or types.
Final Answer:
val flow = MutableSharedFlow<Int>(replay = 2) -> Option B
Quick Check:
Use replay parameter to set past events [OK]
Hint: Use replay parameter in MutableSharedFlow constructor [OK]
Common Mistakes:
Using SharedFlow instead of MutableSharedFlow for creation
Wrong parameter names like bufferSize or replayCount
Confusing replay with buffer
3. What will be the output of this Kotlin code snippet?
val flow = MutableSharedFlow<Int>(replay = 1)
suspend fun main() {
flow.emit(10)
flow.emit(20)
flow.collect { value ->
println(value)
}
}
medium
A. 10
20
B. 10
C. 20
D. No output
Solution
Step 1: Understand replay behavior
With replay = 1, only the last emitted value is kept for new collectors.
Step 2: Analyze emitted values and collection
Values 10 and 20 are emitted, but only 20 is replayed to the collector.
Final Answer:
20 -> Option C
Quick Check:
Replay 1 means last event only [OK]
Hint: Replay 1 means only last emitted event is received [OK]
Common Mistakes:
Expecting all emitted values to be replayed
Confusing replay with buffer size
Thinking collector receives events emitted before subscription
4. Identify the error in this Kotlin code using SharedFlow:
val flow = MutableSharedFlow<Int>()
suspend fun main() {
flow.collect { println(it) }
flow.emit(5)
}
medium
A. collect should be launched in a separate coroutine
B. emit must be called before collect
C. Calling emit after collect blocks the coroutine
D. MutableSharedFlow requires a replay parameter
Solution
Step 1: Understand collect and emit behavior
collect is a suspending function that waits for events and blocks until cancelled.
Step 2: Analyze code flow
Since collect is called first and blocks, emit is never reached. emit must be called concurrently.
Final Answer:
collect should be launched in a separate coroutine -> Option A
Quick Check:
collect blocks; emit needs separate coroutine [OK]
Hint: Run collect and emit in separate coroutines [OK]
Common Mistakes:
Calling emit after collect in same coroutine
Assuming emit runs before collect automatically
Thinking replay parameter is mandatory
5. You want to broadcast user click events to multiple UI components using SharedFlow. Which approach correctly ensures all components receive the latest 3 clicks even if they subscribe late?
hard
A. Use a simple List to store clicks and pass it to components on subscription
B. Use a MutableStateFlow to hold the last click and components read its value
C. Create a Channel with capacity 3 and send clicks; components receive from it
D. Create a MutableSharedFlow with replay = 3 and emit clicks; components collect from it
Solution
Step 1: Identify replay requirement
Replay of 3 means new subscribers get last 3 events automatically.
Step 2: Match with SharedFlow usage
MutableSharedFlow with replay = 3 fits perfectly for broadcasting recent events to multiple listeners.
Step 3: Evaluate other options
MutableStateFlow holds only one value, Channel does not replay past events, List is manual and not reactive.
Final Answer:
Create a MutableSharedFlow with replay = 3 and emit clicks; components collect from it -> Option D
Quick Check:
Replay 3 in SharedFlow broadcasts last 3 events [OK]
Hint: Use MutableSharedFlow with replay for recent event replay [OK]
Common Mistakes:
Using StateFlow which holds only one value
Using Channel which doesn't replay past events
Manually managing event lists instead of reactive streams
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.