Skip to content

Instantly share code, notes, and snippets.

@nickytoh
Last active November 2, 2019 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickytoh/fd88bbf36868507ac69a05ad4be1a3ea to your computer and use it in GitHub Desktop.
Save nickytoh/fd88bbf36868507ac69a05ad4be1a3ea to your computer and use it in GitHub Desktop.
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