Martin Devillers
09/17/2018, 1:01 PMabstract class ScopedAppActivity: AppCompatActivity(), CoroutineScope {
protected 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()
}
}
https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#structured-concurrency-lifecycle-and-coroutine-parent-child-hierarchy
It seems to me that this example is needlessly complicated. The following would work just as well.
abstract class ScopedAppActivity: AppCompatActivity(), CoroutineScope {
override val coroutineContext: CoroutineContext = Job()
override fun onDestroy() {
super.onDestroy()
coroutineContext[Job]?.cancel()
}
}
Since Dispatchers.Main
is the ContinuationInterceptor
used when none is available in a CoroutineContext
, this should yield the same result. Is there something I’m missing?Pavel.AZ
09/17/2018, 1:08 PMMartin Devillers
09/17/2018, 1:09 PM