Coroutines "Create Job" listener


Is there a way to add a listener to scopes creates by Coroutines (global or Scope based)?

My use case is: I have an Android App where I would like to wire Coroutines to Idling Resources due to UI Tests - therefore, if a job is a job is running, the application is idling.

For that, I have two options:

  1. Create an “launchIdling” which would do this wiring;
 fun CoroutineScope.launchIdling(
         context: CoroutineContext = EmptyCoroutineContext,
         start: CoroutineStart = CoroutineStart.DEFAULT,
         block: suspend CoroutineScope.() -> Unit
 ): Job {
     return launch(context, start, block).apply {
         invokeOnCompletion { EspressoIdlingResource.decrement() }
  1. Add some type of “observer” that would know when a job is created and would control this idling system.

Is there a way to accomplish that? Is this considered a bad or good practice on Coroutines world?

Thanks in advance.