Last active
November 2, 2019 16:39
-
-
Save nickytoh/fd88bbf36868507ac69a05ad4be1a3ea to your computer and use it in GitHub Desktop.
Simple example on how to use Coroutines on Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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