Skip to content

Instantly share code, notes, and snippets.

@mayojava
Created October 26, 2018 10:33
Show Gist options
  • Select an option

  • Save mayojava/4c492993c8fbe7978de8817606b4ad4b to your computer and use it in GitHub Desktop.

Select an option

Save mayojava/4c492993c8fbe7978de8817606b4ad4b to your computer and use it in GitHub Desktop.
Android Activity implementing CoroutineScope
class MainActivity: AppCompatActivity(), CoroutineScope {
lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
override fun onDestroy() {
super.onDestroy()
job.cancel() // all children coroutines gets destroyed automatically
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment