Pick the fix
The 'Saved' snackbar sometimes never appears when the user rotates during the save. What's the most robust fix?
class EditorViewModel : ViewModel() {
private val _events = MutableSharedFlow<String>()
val events = _events.asSharedFlow()
fun save() = viewModelScope.launch {
saveToDisk("draft")
_events.emit("Saved")
}
}Hint 1 of 1
What happens to a SharedFlow value when nobody is collecting?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Put the message in UI state and clear it after the UI shows it
Correct. State survives rotation and is only cleared once handled.
Why not the others?
- B Add
replay = 1to the SharedFlow. Now the snackbar shows again for every new collector, including after rotation. - C Emit the event twice. That shows the message twice when nothing is lost.
- D Use
GlobalScopeto emit. The scope that emits isn't the problem; the stream drops values with no collector.