{"id":37001,"date":"2024-11-01T09:53:58","date_gmt":"2024-11-01T09:53:58","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37001"},"modified":"2024-11-01T11:42:39","modified_gmt":"2024-11-01T11:42:39","slug":"course-on-kotlin-android-app-development-activity-anr-issues-and-coroutines","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37001\/","title":{"rendered":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines"},"content":{"rendered":"<p><body><\/p>\n<p>Performance is a crucial factor in Android app development. To improve user experience, the responsiveness of the application is essential. This article will explain the ANR (Application Not Responding) issue and how to solve it using Kotlin&#8217;s coroutines in detail.<\/p>\n<h2>1. What is the ANR (Application Not Responding) Issue?<\/h2>\n<p>ANR occurs when the app&#8217;s UI does not respond for more than 5 seconds while the user is using the app. In this case, the user feels as if the app has frozen, and the system automatically shows a dialog stating &#8220;The app is not responding.&#8221; The main causes of ANR are as follows:<\/p>\n<ul>\n<li>Performing heavy tasks on the main (UI) thread<\/li>\n<li>Response delays due to blocked threads<\/li>\n<li>Synchronization issues between UI updates and data processing<\/li>\n<\/ul>\n<h3>1.1 Example of ANR<\/h3>\n<p>For instance, if a user clicks a button and a network request or a database query is executed on the main thread, ANR can occur. The following is a simple code example that can induce ANR:<\/p>\n<pre><code class=\"language-kotlin\">class MainActivity : AppCompatActivity() {\n        override fun onCreate(savedInstanceState: Bundle?) {\n            super.onCreate(savedInstanceState)\n            setContentView(R.layout.activity_main)\n\n            val button = findViewById<button>(R.id.button)\n            button.setOnClickListener {\n                \/\/ Long task that causes ANR\n                performLongTask()\n            }\n        }\n\n        private fun performLongTask() {\n            \/\/ Sleep for 5 seconds\n            Thread.sleep(5000) \/\/ This causes ANR\n        }\n    }<\/button><\/code><\/pre>\n<h2>2. Introduction to Coroutines<\/h2>\n<p>Coroutines are lightweight threads designed to make asynchronous programming easier. By using coroutines, you can keep your code concise and readable while handling long tasks simultaneously. Coroutines can run tasks separately from the main thread, making them very useful for preventing ANR.<\/p>\n<h3>2.1 Advantages of Coroutines<\/h3>\n<ul>\n<li>Allows intuitive handling of asynchronous tasks<\/li>\n<li>Reduces the complexity of thread management<\/li>\n<li>Improves reusability and ease of testing<\/li>\n<\/ul>\n<h3>2.2 Example of Using Coroutines<\/h3>\n<p>Below is an example of code that uses coroutines to handle long tasks asynchronously:<\/p>\n<pre><code class=\"language-kotlin\">import kotlinx.coroutines.*\n\n    class MainActivity : AppCompatActivity() {\n        override fun onCreate(savedInstanceState: Bundle?) {\n            super.onCreate(savedInstanceState)\n            setContentView(R.layout.activity_main)\n\n            val button = findViewById<button>(R.id.button)\n            button.setOnClickListener {\n                \/\/ Perform long task using coroutines\n                performLongTaskInCoroutine()\n            }\n        }\n\n        private fun performLongTaskInCoroutine() {\n            \/\/ Define a scope to start the coroutine\n            lifecycleScope.launch {\n                \/\/ Asynchronous task\n                delay(5000) \/\/ Wait for 5 seconds\n                \/\/ Update UI after task completion\n                Toast.makeText(this@MainActivity, \"Task Completed!\", Toast.LENGTH_SHORT).show()\n            }\n        }\n    }<\/button><\/code><\/pre>\n<h2>3. Setting Up Coroutines<\/h2>\n<p>To use coroutines, you need to add the required dependencies to Gradle. Modify your build.gradle file to add the coroutine library as follows:<\/p>\n<pre><code class=\"language-plaintext\">dependencies {\n        implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'\n        implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'\n    }<\/code><\/pre>\n<h2>4. CoroutineScope and LifecycleScope<\/h2>\n<p>Coroutines need to be executed within a specific scope. In Android, you can manage coroutines using <code>CoroutineScope<\/code> and <code>LifecycleScope<\/code>. The <code>lifecycleScope<\/code> manages the lifecycle of Activities and Fragments well, allowing for safer operations during UI updates.<\/p>\n<h3>4.1 Example of Using CoroutineScope<\/h3>\n<p>You can utilize CoroutineScope for asynchronous processing, as shown below:<\/p>\n<pre><code class=\"language-kotlin\">class MainActivity : AppCompatActivity(), CoroutineScope {\n        \/\/ Set default dispatcher\n        private val job = Job()\n        override val coroutineContext: CoroutineContext\n            get() = Dispatchers.Main + job\n\n        override fun onDestroy() {\n            super.onDestroy()\n            job.cancel() \/\/ Clean up coroutines when the Activity is destroyed\n        }\n\n        \/\/ Asynchronous processing method\n        private fun performAsyncTask() {\n            launch {\n                \/\/ Asynchronous task\n                withContext(Dispatchers.IO) {\n                    \/\/ Example of a long task\n                    delay(2000)\n                }\n                \/\/ Update UI on the main thread\n                Toast.makeText(this@MainActivity, \"Processing Complete\", Toast.LENGTH_SHORT).show()\n            }\n        }\n    }<\/code><\/pre>\n<h2>5. Using Coroutines to Prevent ANR<\/h2>\n<p>Based on what has been explained so far, you can properly utilize coroutines to solve ANR issues. Below is an example of avoiding ANR problems through a network call:<\/p>\n<pre><code class=\"language-kotlin\">class MainActivity : AppCompatActivity() {\n        override fun onCreate(savedInstanceState: Bundle?) {\n            super.onCreate(savedInstanceState)\n            setContentView(R.layout.activity_main)\n\n            val button = findViewById<button>(R.id.button)\n            button.setOnClickListener {\n                fetchDataFromNetwork()\n            }\n        }\n\n        private fun fetchDataFromNetwork() {\n            lifecycleScope.launch {\n                val result = withContext(Dispatchers.IO) {\n                    \/\/ Simulated network request\n                    simulateNetworkRequest()\n                }\n                \/\/ Update UI\n                Toast.makeText(this@MainActivity, \"Result: $result\", Toast.LENGTH_SHORT).show()\n            }\n        }\n\n        private suspend fun simulateNetworkRequest(): String {\n            delay(3000) \/\/ 3 seconds delay\n            return \"Data Load Complete\"\n        }\n    }<\/button><\/code><\/pre>\n<h2>6. Conclusion<\/h2>\n<p>In this article, we explored the ANR issue and the use of coroutines to solve it in depth. By utilizing coroutines, you can reduce the burden on the UI thread and provide a better experience for users. We encourage you to actively use coroutines to improve both app performance and user experience. We hope this article has been helpful for your Android app development.<\/p>\n<h2>7. Additional Resources<\/h2>\n<p>Below are links to additional resources on coroutines, ANR, and Android app development:<\/p>\n<ul>\n<li><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-overview.html\">Kotlin Coroutines Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/developer.android.com\/kotlin\/coroutines\">Android Coroutines Guide<\/a><\/li>\n<li><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/StrictMode\">Android StrictMode Documentation<\/a><\/li>\n<\/ul>\n<p>If you have questions or feedback, please leave a comment!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Performance is a crucial factor in Android app development. To improve user experience, the responsiveness of the application is essential. This article will explain the ANR (Application Not Responding) issue and how to solve it using Kotlin&#8217;s coroutines in detail. 1. What is the ANR (Application Not Responding) Issue? ANR occurs when the app&#8217;s UI &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37001\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;course on Kotlin Android App Development, Activity ANR Issues and Coroutines&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-37001","post","type-post","status-publish","format-standard","hentry","category-kotlin-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37001\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Performance is a crucial factor in Android app development. To improve user experience, the responsiveness of the application is essential. This article will explain the ANR (Application Not Responding) issue and how to solve it using Kotlin&#8217;s coroutines in detail. 1. What is the ANR (Application Not Responding) Issue? ANR occurs when the app&#8217;s UI &hellip; \ub354 \ubcf4\uae30 &quot;course on Kotlin Android App Development, Activity ANR Issues and Coroutines&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37001\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:53:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:42:39+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"course on Kotlin Android App Development, Activity ANR Issues and Coroutines\",\"datePublished\":\"2024-11-01T09:53:58+00:00\",\"dateModified\":\"2024-11-01T11:42:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/\"},\"wordCount\":470,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37001\/\",\"name\":\"course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:53:58+00:00\",\"dateModified\":\"2024-11-01T11:42:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37001\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37001\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"course on Kotlin Android App Development, Activity ANR Issues and Coroutines\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37001\/","og_locale":"ko_KR","og_type":"article","og_title":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Performance is a crucial factor in Android app development. To improve user experience, the responsiveness of the application is essential. This article will explain the ANR (Application Not Responding) issue and how to solve it using Kotlin&#8217;s coroutines in detail. 1. What is the ANR (Application Not Responding) Issue? ANR occurs when the app&#8217;s UI &hellip; \ub354 \ubcf4\uae30 \"course on Kotlin Android App Development, Activity ANR Issues and Coroutines\"","og_url":"https:\/\/atmokpo.com\/w\/37001\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:53:58+00:00","article_modified_time":"2024-11-01T11:42:39+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37001\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37001\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines","datePublished":"2024-11-01T09:53:58+00:00","dateModified":"2024-11-01T11:42:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37001\/"},"wordCount":470,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37001\/","url":"https:\/\/atmokpo.com\/w\/37001\/","name":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:53:58+00:00","dateModified":"2024-11-01T11:42:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37001\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37001\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37001\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"course on Kotlin Android App Development, Activity ANR Issues and Coroutines"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37001"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37001\/revisions"}],"predecessor-version":[{"id":37002,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37001\/revisions\/37002"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}