Created
October 26, 2018 10:33
-
-
Save mayojava/4c492993c8fbe7978de8817606b4ad4b to your computer and use it in GitHub Desktop.
Android Activity implementing CoroutineScope
This file contains hidden or 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
| 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