Why is `LiveData<Boolean>` bad for one-time ViewModel events?

This tests your grasp of state vs. events. A `LiveData<Boolean>` is state; it re-triggers on rotation. Instead, expose events via a `StateFlow` and have the UI call an `eventConsumed()` function on the ViewModel to maintain unidirectional data flow.
This tests your grasp of state vs. one-time events. `LiveData` holds the last value as state, causing events like Toasts to re-fire after configuration changes (e.g., rotation). A strong answer explains this problem and proposes a solution that maintains unidirectional data flow: the UI observes an event state (e.g., from a `StateFlow`), consumes it, and then calls a ViewModel function like `onEventConsumed()` to signal completion. Simply resetting the LiveData from the UI is a major red flag.
Read the original → developer.android.com
- #android
- #kotlin
- #viewmodel
- #livedata
- #architecture
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.