SharedFlow helps send messages or events to many parts of your app at the same time. It is great for things like showing messages or navigation commands.
You want to show a message (like a toast) after a button click.
You need to tell multiple screens about a change in data.
You want to handle one-time events like navigation or alerts.
You want to share events from a ViewModel to UI components.
You want to avoid missing events when the app screen changes.
Syntax
Android Kotlin
val eventFlow = MutableSharedFlow<Event>()
suspend fun sendEvent(event: Event) {
eventFlow.emit(event)
}
// To collect events
lifecycleScope.launch {
eventFlow.collect { event ->
// handle event
}
}
MutableSharedFlow is used to send events.
Use emit() to send an event and collect() to receive it.
Examples
Sends a string message as an event to show a toast.
Android Kotlin
val toastEvent = MutableSharedFlow<String>()
suspend fun showToast(message: String) {
toastEvent.emit(message)
}
Collects the toast messages and shows them on screen.
Hint: emit() and collect() must be called in coroutines [OK]
Common Mistakes:
Overlooking that emit() and collect() are suspend functions
Assuming the code compiles and runs to test replay behavior
Forgetting coroutine requirements for Flow operations
4. Identify the problem in this code snippet using SharedFlow:
val eventFlow = MutableSharedFlow<String>()
fun start() {
eventFlow.emit("Start")
}
fun listen() {
eventFlow.collect { println(it) }
}
start()
listen()
medium
A. No problem, code works fine
B. collect() is called without a coroutine
C. MutableSharedFlow cannot emit strings
D. emit() is called outside a coroutine
Solution
Step 1: Check emit() usage
emit() is a suspend function and must be called inside a coroutine.
Step 2: Check collect() usage
collect() is also suspend and needs a coroutine, but the question focuses on emit() first.
Final Answer:
emit() is called outside a coroutine -> Option D
Quick Check:
emit() requires coroutine context [OK]
Hint: emit() must run inside a coroutine [OK]
Common Mistakes:
Calling emit() directly without coroutine
Ignoring suspend nature of collect()
Thinking MutableSharedFlow restricts data types
5. You want to show a toast message every time a button is clicked using SharedFlow. Which approach correctly ensures the toast shows for each click even if the UI is recreated?
hard
A. Use a MutableSharedFlow with no replay and emit on button click
B. Use a MutableSharedFlow with replay = 1 and emit on button click
C. Use a StateFlow to hold the last toast message and emit on button click
D. Use LiveData to send the toast message on button click
Solution
Step 1: Understand event replay needs
To show toast after UI recreation, events must replay at least once.
Step 2: Choose correct flow type and replay
MutableSharedFlow with replay=1 replays last event to new collectors, ensuring toast shows.
Step 3: Compare other options
No replay means missed events; StateFlow holds state, not events; LiveData is not SharedFlow.
Final Answer:
Use a MutableSharedFlow with replay = 1 and emit on button click -> Option B
Quick Check:
Replay 1 ensures event delivery after UI recreation [OK]
Hint: Set replay=1 to resend last event to new collectors [OK]
Common Mistakes:
Using no replay and missing events after UI changes
Confusing StateFlow state with event replay
Using LiveData instead of SharedFlow for events
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.