Answer the question
In order to leave comments, you need to log in
How to DI for application context?
I want to get a Glide instance, which needs a context to create.
My AppComponent:
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
@Component.Factory
interface Factory{
fun create(@BindsInstance applicationContext: Context): AppComponent
}
fun getPreviewComponent(): PreviewComponent.Factory
fun getDetailComponent(): DetailComponent.Factory
fun inject(mainActivity: MainActivity)
fun inject(searchSettingsFragment: SearchSettingsFragment)
}
class BaseApplication : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.factory().create(applicationContext)
}
}
@Module
class AppModule () {
@Provides
fun provideApplicationContext(applicationContext: Context): Context {
return applicationContext
}
@Singleton
@Provides
fun provideGlideInstance(applicationContext: Context): Glide {
return Glide.get(applicationContext)
}
}
Answer the question
In order to leave comments, you need to log in
He writes to you that the cycle is in the graph.
Provides
fun provideApplicationContext(applicationContext: Context): Context
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question