{"id":55988,"date":"2024-01-03T16:09:29","date_gmt":"2024-01-03T10:39:29","guid":{"rendered":"https:\/\/www.oneclickitsolution.com\/blog\/?p=55988"},"modified":"2024-01-03T16:09:31","modified_gmt":"2024-01-03T10:39:31","slug":"kotlin-coroutines-in-android","status":"publish","type":"post","link":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android","title":{"rendered":"Fundamentals of Using Kotlin Coroutines in Android"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-introduction\">Introduction<\/h2>\n\n\n\n<p>In this world, what do people expect from technology? We expect that all things should be done efficiently and quickly. This can be achieved by a procedure known as&nbsp;Multitasking.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multitasking has 2 types of methods, like<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Operating systems manage the flow between operations.<\/li>\n\n\n\n<li>This second type is called Cooperative multitasking, Operations control changes by itself.<\/li>\n<\/ol>\n\n\n\n<p>For that reason in the kotlin language, There is one concept called <strong>Coroutine<\/strong>.&nbsp;<\/p>\n\n\n\n<p>Coroutines were launched to Kotlin in <strong>version 1.3<\/strong>. Coroutine is basically a multitasking method. By use of this, we can perform multiple simultaneous processes. A coroutine is not a thread, a Coroutine is normally the same as a lightweight thread.&nbsp;<\/p>\n\n\n\n<p>In one thread, we can access the multiple coroutines. A coroutine can exchange the thread from one to another. But Sometimes, If the Programmer can\u2019t use the coroutine in the proper way then it\u2019ll create some issues. Like memory-related problems, Unnecessary problems will be created.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.oneclickitsolution.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"275\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/Android-Custom-Library-CTA2.png\" alt=\"Android Custom Library CTA2\" class=\"wp-image-54440\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/Android-Custom-Library-CTA2.png 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/Android-Custom-Library-CTA2-768x176.png 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/12\/Android-Custom-Library-CTA2-20x5.png 20w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<p>Before starting the Demo of the Coroutine, you should have to clear some coroutine-related topics. Coroutine has some topic which is described below: <strong>Scopes, Dispatchers, and Suspend Functions.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Coroutine Scopes<\/h2>\n\n\n\n<p>The scope of<a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-overview.html\" target=\"_blank\" rel=\"noreferrer noopener\"> Kotlin coroutines<\/a> can be defined as the constraints within which the coroutines are executed. Scopes help in modelling the lifecycle of coroutines. Kotlin coroutines have three basic scopes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Global Scope<\/li>\n\n\n\n<li>LifeCycle Scope<\/li>\n\n\n\n<li>ViewModel Scope<\/li>\n<\/ul>\n\n\n\n<p>We can declare the dispatches in the Scope, at the end of all tasks we have to close that coroutine operation in scope. For this purpose, we can use the Scopes. If we destroy the \u201cScope\u201d then all the running tasks of that particular scope will also destroy with that Scope.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Coroutine Dispatchers<\/h2>\n\n\n\n<p>Dispatchers are given the context of the coroutine code. So coroutine finds the thread where they are executed. Dispatchers have 4 types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Main Dispatchers<\/li>\n\n\n\n<li>IO Dispatchers<\/li>\n\n\n\n<li>Default Dispatchers<\/li>\n\n\n\n<li>Unconfined Dispatcher<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Main Dispatchers<\/h3>\n\n\n\n<p>Main dispatchers execute the coroutine on the <strong>main thread<\/strong>. The main dispatchers do most of the work on UI. It\u2019s also known as a <strong>UI thread<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. IO Dispatchers<\/h3>\n\n\n\n<p>IO Dispatchers initiate the coroutine on the <strong>IO thread<\/strong>, which helps to perform all data operations such as the internet, either reading or writing from the database, reading or writing to files, and so on. Data retrieval from a database is an IO operation performed on the IO thread.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Default Dispatchers<\/h3>\n\n\n\n<p>Default dispatchers work on the <strong>default thread<\/strong>. When we want to do intricate and prolonged calculations that may cause the main thread to lag and the user display to become inaccessible, that\u2019s why we should use this dispatcher.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Unconfined Dispatcher<\/h3>\n\n\n\n<p>Unconfined Dispatcher does not work on any particular thread. without requiring any particular threading strategy, permits the coroutine to restart in whatever thread is being utilized by the associated suspending function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Suspended Function<\/h2>\n\n\n\n<p>A function called suspend has three possible states: begun, stopped, and resumed. One of the most crucial things to keep in mind regarding the suspend functions is that they may only be invoked from another suspension function or a coroutine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Let\u2019s Start The Demo<\/h2>\n\n\n\n<p>We are creating one demo project to better understand the coroutine\u2019s work. In this project, we have 2 counters which increase from 1 to 10. the 1st counter will work at a 1-second delay. On the other hand, the 2nd one will work at a 3-second delay.<\/p>\n\n\n\n<p>At the bottom, there are 2 buttons, one for starting the process and the second one for stopping the counting. After the process there is one pop-up toast will be generated. So this is the work of our demo.<\/p>\n\n\n\n<p>First of all, you have to start with setting the dependency and Gradle settings. Which are shown below:<\/p>\n\n\n\n<p>\/\/build.gradle<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">plugins {\n   id 'com.android.application'\n   id 'org.jetbrains.kotlin.android'\n}\nandroid {\n   compileSdk 32\n   defaultConfig {\n       applicationId \"com.example.highlevelcoroutine\"\n       minSdk 22\n       targetSdk 32\n       versionCode 1\n       versionName \"1.0\"\n       testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n   }\n   buildTypes {\n       release {\n           minifyEnabled false\n           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'\n       }\n   }\n   buildFeatures{\n       dataBinding true\n   }\n   compileOptions {\n       sourceCompatibility JavaVersion.VERSION_1_8\n       targetCompatibility JavaVersion.VERSION_1_8\n   }\n   kotlinOptions {\n       jvmTarget = '1.8'\n   }\n}\n\ndependencies {\n   implementation 'androidx.core:core-ktx:1.7.0'\n   implementation 'androidx.appcompat:appcompat:1.5.1'\n   implementation 'com.google.android.material:material:1.6.1'\n   implementation 'androidx.constraintlayout:constraintlayout:2.1.4'\n   testImplementation 'junit:junit:4.13.2'\n   androidTestImplementation 'androidx.test.ext:junit:1.1.3'\n   androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'\n   implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'\n}\n<\/code><\/pre>\n\n\n\n<p>If there is some dependency that will be deprecated then you can check from android\u2019s documentation.<\/p>\n\n\n\n<p>After the dependency work, We will start coding.<\/p>\n\n\n\n<p>First create the main activity and its layout. So we create on UI XML file and give the name like activity_main.xml, Its code is below:<\/p>\n\n\n\n<p><strong>\/\/activity_main.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;layout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n   xmlns:tools=\"http:\/\/schemas.android.com\/tools\"&gt;\n\n   &lt;data&gt;\n\n   &lt;\/data&gt;\n\n   &lt;LinearLayout\n       android:layout_width=\"match_parent\"\n       android:layout_height=\"match_parent\"\n       android:weightSum=\"3\"\n       android:orientation=\"vertical\"\n       tools:context=\".MainActivity\"&gt;\n\n       &lt;LinearLayout\n           android:layout_width=\"match_parent\"\n           android:layout_height=\"wrap_content\"\n           android:layout_weight=\"1\"\n           android:gravity=\"center\"\n           android:orientation=\"horizontal\"\n           android:background=\"@android:color\/holo_blue_dark\"&gt;\n\n           &lt;TextView\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:layout_marginEnd=\"20dp\"\n               android:text=\"@string\/the_counter_first_text\"\n               android:textSize=\"32sp\"\n               android:textColor=\"@color\/white\" \/&gt;\n\n           &lt;TextView\n               android:id=\"@+id\/the_first_counter_tv\"\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:text=\"0\"\n               android:textSize=\"30sp\"\n               android:textColor=\"@android:color\/white\" \/&gt;\n\n       &lt;\/LinearLayout&gt;\n\n       &lt;LinearLayout\n           android:layout_width=\"match_parent\"\n           android:layout_height=\"wrap_content\"\n           android:layout_weight=\"1\"\n           android:gravity=\"center\"\n           android:orientation=\"horizontal\"\n           android:background=\"@android:color\/black\"&gt;\n\n           &lt;TextView\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:layout_marginEnd=\"20dp\"\n               android:text=\"@string\/the_counter_second_text\"\n               android:textSize=\"32sp\"\n               android:textColor=\"@color\/white\" \/&gt;\n\n           &lt;TextView\n               android:id=\"@+id\/the_second_counter_tv\"\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:text=\"0\"\n               android:textSize=\"30sp\"\n               android:textColor=\"@android:color\/white\" \/&gt;\n\n       &lt;\/LinearLayout&gt;\n\n       &lt;LinearLayout\n           android:layout_width=\"match_parent\"\n           android:layout_height=\"wrap_content\"\n           android:layout_weight=\"1\"\n           android:gravity=\"center\"\n           android:orientation=\"horizontal\"\n           android:background=\"@android:color\/darker_gray\"&gt;\n\n           &lt;Button\n               android:id=\"@+id\/btn_start\"\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:layout_marginEnd=\"20dp\"\n               android:backgroundTint=\"@android:color\/holo_green_dark\"\n               android:text=\"@string\/start_text\"\n               android:textAllCaps=\"false\" \/&gt;\n\n           &lt;Button\n               android:id=\"@+id\/btn_stop\"\n               android:layout_width=\"wrap_content\"\n               android:layout_height=\"wrap_content\"\n               android:backgroundTint=\"@android:color\/holo_red_dark\"\n               android:text=\"@string\/stop_text\"\n               android:textAllCaps=\"false\" \/&gt;\n\n\n       &lt;\/LinearLayout&gt;\n\n   &lt;\/LinearLayout&gt;\n&lt;\/layout&gt;<\/code><\/pre>\n\n\n\n<p>After completing the UI-related work we go to the MainActivity.kt File:<\/p>\n\n\n\n<p><strong>\/\/MainActivity.kt<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">package com.example.highlevelcoroutine\n\nimport android.os.Bundle\nimport android.widget.Toast\nimport androidx.appcompat.app.AppCompatActivity\nimport androidx.lifecycle.Observer\nimport androidx.lifecycle.ViewModelProvider\nimport com.example.highlevelcoroutine.databinding.ActivityMainBinding\n\nclass MainActivity : AppCompatActivity() {\n   \/\/for data binding\n   private lateinit var activityMainBinding: ActivityMainBinding\n   private lateinit var counterViewModel: CounterViewModel\n\n   override fun onCreate(savedInstanceState: Bundle?) {\n       super.onCreate(savedInstanceState)\n       activityMainBinding = ActivityMainBinding.inflate(layoutInflater)\n       setContentView(activityMainBinding.root)\n       counterViewModel = ViewModelProvider(this)[CounterViewModel::class.java]\n       counterViewModel.counter1Toast.observe(this@MainActivity, Observer {\n           activityMainBinding.theFirstCounterTv.text = it.toString()\n       })\n       counterViewModel.counter2Toast.observe(this@MainActivity, Observer {\n           activityMainBinding.theSecondCounterTv.text = it.toString()\n       })\n       counterViewModel.showToast.observe(this@MainActivity, Observer {\n           Toast.makeText(this, it, Toast.LENGTH_SHORT).show()\n       })\n       activityMainBinding.btnStart.setOnClickListener {\n           counterViewModel.startCounters()\n       }\n       activityMainBinding.btnStop.setOnClickListener {\n           counterViewModel.stopCounters()\n       }\n   }\n}\n<\/code><\/pre>\n\n\n\n<p>In this, we will create one variable for our binding and another variable for our viewmodel.<\/p>\n\n\n\n<p>Before that, we will create our ViewModel and give its name like CounterViewModel.kt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">package com.example.highlevelcoroutine\n\nimport androidx.lifecycle.LiveData\nimport androidx.lifecycle.MutableLiveData\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\nimport kotlinx.coroutines.Job\nimport kotlinx.coroutines.delay\nimport kotlinx.coroutines.joinAll\nimport kotlinx.coroutines.launch\n\nclass CounterViewModel : ViewModel() {\n\n   private fun showToast(message: String) {\n\n       _showToast.value = message\n   }\n\n   private var counter1 = MutableLiveData(0)\n   private var counter2 = MutableLiveData(0)\n   private var _showToast = MutableLiveData(\"\")\n\n   private var counter1Job: Job? = null\n   private var counter2Job: Job? = null\n\n   var counter1Toast: LiveData&lt;Int&gt; = counter1\n   var counter2Toast: LiveData&lt;Int&gt; = counter2\n   var showToast: LiveData&lt;String&gt; = _showToast\n\n   fun startCounters() {\n       counter1.value =0\n       counter2.value =0\n       counter1Job = viewModelScope.launch {\n           for (i in 1..5) {\n               counter1.value = (counter1.value ?: 0) + 1\n               delay(1000)\n           }\n       }\n       counter2Job = viewModelScope.launch {\n           for (i in 1..5) {\n               counter2.value = (counter2.value ?: 0) + 1\n               delay(3000)\n           }\n       }\n       counter1Job?.let { job1 -&gt;\n           counter2Job?.let { job2 -&gt;\n               val listOfJobs = listOf(job1, job2)\n               viewModelScope.launch {\n                   listOfJobs.joinAll()\n                   showToast(\"Counters are completed.\")\n               }\n           }\n       }\n   }\n   fun stopCounters() = viewModelScope.launch {\n       counter1Job?.cancel()\n       counter2Job?.cancel()\n   }\n}<\/code><\/pre>\n\n\n\n<p>Here, The main work of the <a href=\"https:\/\/www.oneclickitsolution.com\/services\/mobile-app-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">application<\/a> will be done in this area. Creates two MutableLiveData initialized with the given value. And give it names like <em>counter1<\/em> and <em>counter2<\/em>. Make sure that these variables should be var and private. Create another variable for toast, this variable is the same as <em>counter1<\/em> and <em>counter2<\/em> but its value is not given like them, It\u2019s a string.&nbsp;<\/p>\n\n\n\n<p>And after this, for observing these variables in the main activity we are assigning these variables to the livedata variables. Make <em>counter1Toast<\/em>, <em>counter2Toast<\/em> and showToast and assign the above 3 MutableLiveData variables here.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.oneclickitsolution.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"275\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/10\/Android-vs-iOS-Mobile-App-CTA-1.png\" alt=\"Android vs iOS Mobile App\" class=\"wp-image-54323\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/10\/Android-vs-iOS-Mobile-App-CTA-1.png 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/10\/Android-vs-iOS-Mobile-App-CTA-1-768x176.png 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2021\/10\/Android-vs-iOS-Mobile-App-CTA-1-20x5.png 20w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<p>After finishing the variable work, We will create one function called <strong>startCounter()<\/strong>. First set the value 0 to both counter variable 1&amp;2. We have 2 types of counters, One will work on 1 sec delay period, and the Second one will work on 3 sec delay period. For this reason, we will create 2 variables of JOB. We will assign it as a null. <em>counter1Job<\/em> will work on 1 sec delay period and <em>counter2Job<\/em> will work on 3 sec delay period. We use the for loop to increment the counter to the 5 counts. With 1 and 3 sec delay. We will do this in Scope, <strong>viewmodelscope<\/strong> will use it for our processes.&nbsp;<\/p>\n\n\n\n<p>We will join both jobs cause toast should be shown if both jobs are done or canceled. For this, we use joinAll() function. JoinAll() is work to combine all jobs, If any job is not complete yet then it will not finish the process. So for that reason, we will scope function <strong>\u201clet\u201d<\/strong> and create one variable and assign the list of both jobs. And after this in <strong>viewmodelscope<\/strong>, we will join the list of both jobs. For showing the toast we will create one small function for toast. Give the name showToast() and put the parameter message and set it as a string. In this function assign the message to the _showToast MutableLiveData. It will show that toast when both jobs will complete.&nbsp;<\/p>\n\n\n\n<p>To stop the counting process, We will create one function called <strong>stopCounters()<\/strong>. In this, we will cancel both jobs. So automatically it will stop increasing the counting. For this reason, we will use the cancel() function. It will help to cancel both jobs.<\/p>\n\n\n\n<p>Here the main work is done. After completing this coding, we will go back to the Main activity to observe and set all live data variables to the text view and buttons.<\/p>\n\n\n\n<p>First, we can use the created variable counterViewModel and get the ViewModel Instance. By <strong>ViewModelProvider()<\/strong>. Observe the <em>counter1Toast<\/em> and set the value to the theFirstCounterTv text view. Same as for <em>counter2Toast<\/em>, we will assign the value of it to the theSecondCounterTv text view. Observe the showToast variable and set the value to the Toast.<\/p>\n\n\n\n<p>Then go set the onClick of the both start and stop process buttons. startCounters() will be used in the btnStart button and stopCounters() will be used in btnStop button\u2019s OnClick event.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Outputs<\/h2>\n\n\n\n<p>First event when completing both processes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"216\" height=\"384\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/First-event-when-completing-both-processes.gif\" alt=\"First event when completing both processes\" class=\"wp-image-55992\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/First-event-when-completing-both-processes.gif 216w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/First-event-when-completing-both-processes-11x20.gif 11w\" sizes=\"(max-width: 216px) 100vw, 216px\" \/><\/figure>\n\n\n\n<p>The second event, Stop processes in between execution.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/Second-event-Stop-processes-in-between-execution.gif\" alt=\"Second event Stop processes in between execution - Kotlin Coroutines\" class=\"wp-image-55993\" width=\"216\" height=\"384\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/Second-event-Stop-processes-in-between-execution.gif 154w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/Second-event-Stop-processes-in-between-execution-11x20.gif 11w\" sizes=\"(max-width: 216px) 100vw, 216px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">To conclude<\/h2>\n\n\n\n<p>We hope that you found this blog useful for learning about coroutines in <a href=\"https:\/\/www.oneclickitsolution.com\/services\/android-app-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Android<\/a> and will be able to use them confidently in your own projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this world, what do people expect from technology? We expect that all things should be done efficiently and quickly. This can be achieved by a procedure known as&nbsp;Multitasking. Multitasking has 2 types of methods, like Operating systems manage the flow between operations. This second type is called Cooperative multitasking, Operations control changes by &hellip;<\/p>\n","protected":false},"author":77,"featured_media":55997,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[516],"tags":[901,902],"class_list":["post-55988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-apps","tag-android","tag-kotlin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.2.1 (Yoast SEO v24.8.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Fundamentals of Using Kotlin Coroutines in Android<\/title>\n<meta name=\"description\" content=\"We hope that you found this blog useful for learning about using kotlin coroutines in android and using them confidently in your projects.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fundamentals of Using Kotlin Coroutines in Android\" \/>\n<meta property=\"og:description\" content=\"We hope that you found this blog useful for learning about using kotlin coroutines in android and using them confidently in your projects.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android\" \/>\n<meta property=\"og:site_name\" content=\"OneClick IT Consultancy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/oneclickconsultancy\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-03T10:39:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-03T10:39:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Abhishek Oza\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:site\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Oza\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Fundamentals of Using Kotlin Coroutines in Android","description":"We hope that you found this blog useful for learning about using kotlin coroutines in android and using them confidently in your projects.","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:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android","og_locale":"en_US","og_type":"article","og_title":"Fundamentals of Using Kotlin Coroutines in Android","og_description":"We hope that you found this blog useful for learning about using kotlin coroutines in android and using them confidently in your projects.","og_url":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android","og_site_name":"OneClick IT Consultancy","article_publisher":"https:\/\/www.facebook.com\/oneclickconsultancy","article_published_time":"2024-01-03T10:39:29+00:00","article_modified_time":"2024-01-03T10:39:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png","type":"image\/png"}],"author":"Abhishek Oza","twitter_card":"summary_large_image","twitter_creator":"@OneclickIT","twitter_site":"@OneclickIT","twitter_misc":{"Written by":"Abhishek Oza","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#article","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android"},"author":{"name":"Abhishek Oza","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/71a995b022f9894449a29f9ab4ec66e9"},"headline":"Fundamentals of Using Kotlin Coroutines in Android","datePublished":"2024-01-03T10:39:29+00:00","dateModified":"2024-01-03T10:39:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android"},"wordCount":1256,"publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png","keywords":["Android","Kotlin"],"articleSection":["Mobile Application"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android","url":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android","name":"Fundamentals of Using Kotlin Coroutines in Android","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#primaryimage"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png","datePublished":"2024-01-03T10:39:29+00:00","dateModified":"2024-01-03T10:39:31+00:00","description":"We hope that you found this blog useful for learning about using kotlin coroutines in android and using them confidently in your projects.","breadcrumb":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#primaryimage","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/09\/using-kotlin-coroutines-in-android.png","width":1200,"height":628,"caption":"using kotlin coroutines in android"},{"@type":"BreadcrumbList","@id":"https:\/\/www.oneclickitsolution.com\/blog\/kotlin-coroutines-in-android#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.oneclickitsolution.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Fundamentals of Using Kotlin Coroutines in Android"}]},{"@type":"WebSite","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website","url":"https:\/\/www.oneclickitsolution.com\/blog\/","name":"OneClick IT Consultancy","description":"We Build Brands from Ideas","publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"alternateName":"OneClick IT Solution","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oneclickitsolution.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization","name":"OneClick IT Consultancy","alternateName":"OneClick IT Solution","url":"https:\/\/www.oneclickitsolution.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","width":100,"height":100,"caption":"OneClick IT Consultancy"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/oneclickconsultancy","https:\/\/x.com\/OneclickIT","https:\/\/www.instagram.com\/oneclick.it.consultancy\/","https:\/\/www.linkedin.com\/company\/one-click-it-consultancy\/","https:\/\/www.pinterest.com\/oneclickitconsultancy\/","https:\/\/www.youtube.com\/channel\/UCsEG6aiwOwvYrcZxMoP5xjg","https:\/\/oneclickit.tumblr.com\/","https:\/\/dribbble.com\/oneclickitconsultancy"]},{"@type":"Person","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/71a995b022f9894449a29f9ab4ec66e9","name":"Abhishek Oza","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a918bc5f9e0daecc14e1076369bed383?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a918bc5f9e0daecc14e1076369bed383?s=96&d=mm&r=g","caption":"Abhishek Oza"},"description":"I am a Fresher Android Developer with a proven track record of working at OneClick IT Consultancy. I have knowledge of mobile app development and I am pursuing the 2nd Year of my Btech Degree at SVBIT (Shankersinh Vaghela Bapu Institute of Technology). I want to explore more in Android and other Hybrid Languages.","sameAs":["https:\/\/www.linkedin.com\/in\/abhishek-oza-071019227\/"],"url":"https:\/\/www.oneclickitsolution.com\/blog\/author\/abhishekoza"}]}},"_links":{"self":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/55988"}],"collection":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/comments?post=55988"}],"version-history":[{"count":0,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/55988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media\/55997"}],"wp:attachment":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media?parent=55988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/categories?post=55988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/tags?post=55988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}