{"id":1905,"date":"2025-07-30T20:00:00","date_gmt":"2025-07-30T20:00:00","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1905"},"modified":"2026-02-05T12:00:06","modified_gmt":"2026-02-05T12:00:06","slug":"handle-one-time-events-in-android-the-clean-way","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/handle-one-time-events-in-android-the-clean-way\/","title":{"rendered":"How to Handle One-Time Events Like Toasts or Navigation in Android"},"content":{"rendered":"\n<p>Ever seen the same toast pop up again after a screen rotation? Or a navigation event fire twice? That\u2019s a common problem when using LiveData for Android.<\/p>\n\n\n\n<p><strong>Challenge:<\/strong><\/p>\n\n\n\n<p>LiveData can re\u2011emit its last value on configuration changes, causing duplicate toasts or navigation events.<\/p>\n\n\n\n<p><strong>Solution<\/strong>:<\/p>\n\n\n\n<p>Wrap events in a consumable Event class or use Kotlin Channels\/Flow for single-shot events.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kotlin\nopen class Event&lt;out T>(private val content: T) {\n    private var handled = false\n    fun getContentIfNotHandled(): T? =\n        if (handled) null else { handled = true; content }\n}\n\n\/\/ In ViewModel\nprivate val _navigate = MutableLiveData&lt;Event&lt;NavDirections>>()\nval navigate: LiveData&lt;Event&lt;NavDirections>> = _navigate<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p>When you only want an event to happen once, don\u2019t rely on plain LiveData. Wrapping your data in an Event class or using Kotlin\u2019s Flow or Channels helps you avoid glitches like duplicate toasts or repeated navigation. If this kind of clean architecture matters to your app, it\u2019s worth having experienced <a href=\"https:\/\/www.cmarix.com\/hire-android-developers.html\">Android engineers<\/a> on your team who know how to handle these edge cases correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever seen the same toast pop up again after a screen rotation? Or a navigation event fire twice? That\u2019s a common problem when using LiveData for Android. Challenge: LiveData can re\u2011emit its last value on configuration changes, causing duplicate toasts or navigation events. Solution: Wrap events in a consumable Event class or use Kotlin Channels\/Flow [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1923,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9,1],"tags":[],"class_list":["post-1905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mobile"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1905"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"predecessor-version":[{"id":1907,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1905\/revisions\/1907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1923"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}