Skip to content

Instantly share code, notes, and snippets.

@nickytoh

nickytoh/MyActivity.kt

Last active Nov 2, 2019
Embed
What would you like to do?
Simple example on how to use Coroutines on Android.
// Activity::onCreate
val job = Job()
val coroutineScope = CoroutineScope(Dispatchers.IO + job)
coroutineScope.launch {
val json = URL("https://jsonplaceholder.typicode.com/posts?_limit=5").readText()
withContext(Dispatchers.Main) {
findViewById<TextView>(R.id.title).text = json
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment