Skip to main content

Posts

Showing posts with the label Android Architecture

Kotlin Coroutines: Safely Handling One-Off UI Events (Channel vs SharedFlow)

  It is the most persistent headache in modern Android development: handling "fire-and-forget" events. You trigger a navigation action, show a Toast, or display a Snackbar. It works perfectly until the user rotates their device or switches to Dark Mode. Suddenly, that "Payment Successful" Snackbar pops up a second time (the "Zombie Event"). Or worse, the navigation event triggers while the app is in the background, effectively vanishing into the void, leaving the user stuck on a loading screen. Using  LiveData  for events was deprecated years ago.  StateFlow  is for  state , not events. The battleground for one-off events is now between  SharedFlow  and  Channel . If you don’t understand the underlying mechanics of how these primitives buffer and replay data, your app likely contains race conditions waiting to happen. Here is the architectural pattern for handling single-shot UI events that ensures they are delivered exactly once, even across...