Problem: sequential output when executing a function in kotlin

many thanks to everyone! and especially kyay10. I will do this

1 Like

Unfortunately, I can’t make a working code. Dear kyay10 , please give a working example

First of all add a depndency on kotlinx.coroutines and on lifecycle-runtime in your build.gradle (i.e. just do this:

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
    // Rest of dependencies go here
}

Then, replace your for loop with this:

lifecycleScope.launch {
    for (x1 in 1…10) {
        println(x1) // replace with TextView code
        delay(1000) // wait for 1 second
    }
}
`

That’s great! It works! Thank you very much!
But it was necessary to add

import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
1 Like

Yeah, most people online operate under the assumption that people use a good enough ide that can at least auto-import lol. I should’ve probably included these though, so thank you for that.

1 Like