Create an application CoroutineScope using Hilt
<p>In this article, you’ll learn how to create an application-scoped <code>CoroutineScope</code> using Hilt, and how to inject it as a dependency. To further improve the way we work with Coroutines, we’ll see how to inject the different <code>CoroutineDispatcher</code>s and replace their implementations in tests.</p>
<h1>Manual dependency injection</h1>
<p>To create an <a href="https://medium.com/androiddevelopers/scoping-in-android-and-hilt-c2e5222317c0" rel="noopener">application-scoped</a> <code>CoroutineScope</code> following dependency injection (DI) best practices <a href="https://developer.android.com/training/dependency-injection/manual" rel="noopener ugc nofollow" target="_blank">manually</a> without any library, you’d typically add a new variable to your application class with an instance of a <code>CoroutineScope</code>. The same instance would be manually passed around when creating other objects.</p>
<p><iframe frameborder="0" height="260" scrolling="no" src="https://medium.com/media/9dc4e40b6cf16570d8efcd62976a7a82" title="MyApplication.kt" width="680"></iframe></p>
<p>Since there isn’t a reliable way to know when the <code>Application</code> is destroyed in Android, you don’t need to call <code>applicationScope.cancel()</code> manually as the scope and all ongoing work will be destroyed when the application process finishes.</p>
<p><a href="https://medium.com/androiddevelopers/create-an-application-coroutinescope-using-hilt-dd444e721528">Website</a></p>