{"id":1880,"date":"2025-07-30T11:49:09","date_gmt":"2025-07-30T11:49:09","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1880"},"modified":"2026-02-05T12:00:14","modified_gmt":"2026-02-05T12:00:14","slug":"handle-api-failures-in-android-mvvm-gracefully","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/handle-api-failures-in-android-mvvm-gracefully\/","title":{"rendered":"How to Handle API Failures Gracefully in Android MVVM Architecture"},"content":{"rendered":"\n<p>When an API call fails, showing a blank screen or crashing the app ruins the user experience. Here\u2019s a clean way to manage those failures in MVVM using Kotlin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Challenges is facing in Android MVVM Architecture<\/h2>\n\n\n\n<p>If an API call fails because of network issues or server problems, many apps either crash or show a blank screen. How do you make sure users still see something useful?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Solution<\/h3>\n\n\n\n<p>Use a sealed class like Resource to handle different outcomes: Success, Error, and Loading. Then connect it with LiveData or StateFlow in your ViewModel to update the UI based on each case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kotlin\n\/\/ 1. Define the Resource wrapper\nsealed class Resource&lt;T> {\n    data class Success&lt;T>(val data: T): Resource&lt;T>()\n    data class Error&lt;T>(val message: String): Resource&lt;T>()\n    class Loading&lt;T>: Resource&lt;T>()\n}\n\n\/\/ 2. Repository method\nsuspend fun getData(): Resource&lt;User> = try {\n    val response = api.getUser()\n    Resource.Success(response)\n} catch (e: Exception) {\n    Resource.Error(e.localizedMessage ?: \"Unknown Error\")\n}\n\n\/\/ 3. ViewModel usage\nval user = MutableLiveData&lt;Resource&lt;User>>()\n\nfun fetchUser() = viewModelScope.launch {\n    user.value = Resource.Loading()\n    user.value = repository.getData()\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Handling API failures properly isn&#8217;t optional. It&#8217;s basic hygiene for a smooth user experience. If you&#8217;re building serious apps or planning to <a href=\"https:\/\/www.cmarix.com\/hire-android-developers.html\">hire Android developer<\/a> talent, wrapping your responses in a Resource class helps keep your code cleaner and your UI more responsive to real-world issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When an API call fails, showing a blank screen or crashing the app ruins the user experience. Here\u2019s a clean way to manage those failures in MVVM using Kotlin. What Challenges is facing in Android MVVM Architecture If an API call fails because of network issues or server problems, many apps either crash or show [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1915,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9,1],"tags":[],"class_list":["post-1880","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\/1880","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=1880"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1880\/revisions"}],"predecessor-version":[{"id":1882,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1880\/revisions\/1882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1915"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}