Skip to main content

android kotlin - Coroutines start undispatched







MainActivity.kt



package com.example.coroutine

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

button.setOnClickListener {
// set text color by checkbox checked state
if (checkBox.isChecked){
textView.setTextColor(Color.parseColor("#800020"))
textView.text = "1. CoroutineStart.UNDISPATCHED"
}
else{
textView.setTextColor(Color.parseColor("#8DB600"))
textView.text = "1. CoroutineStart.DEFAULT"
}


GlobalScope.launch(
Dispatchers.Main ,
if (checkBox.isChecked){
// immediately executes coroutine until its first
// suspension point in the current thread
CoroutineStart.UNDISPATCHED
}else{
// immediately schedules coroutine for execution
// according to its context
CoroutineStart.DEFAULT
}
) {
textView.append("\n\n2. Inside coroutine, before delay.")
delay(5000)
textView.append("\n\n3. Delayed 5 seconds.")
}

textView.append("\n\n4. Outside coroutine, after coroutine.")
}
}
}




activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#EDEAE0">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
tools:text="Hello World!"
style="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintHorizontal_bias="0.534"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Click Me"
android:backgroundTint="#4B5320"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="UNDISPATCHED"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>




build.gradle (app) [dependencies]



// kotlin coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'














More android kotlin tutorials