简体   繁体   中英

How to inject Context into a Presenter using Dagger 2

I've been checking recently Dagger 2.14.1 with the new Android injectors.

I'm using MVP and the Presenter is getting inject into the View correctly:

class CustomApplication : Application(), HasActivityInjector {

    @Inject
    lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        DaggerApplicationComponent
                .builder()
                .create(this)
                .inject(this)
    }

    override fun activityInjector(): DispatchingAndroidInjector<Activity> {
        return activityDispatchingAndroidInjector
    }
}

--

@Singleton
@Suppress("UNUSED")
@Component(modules = arrayOf(AndroidInjectionModule::class, ApplicationModule::class, ActivityBuilder::class))
interface ApplicationComponent : AndroidInjector<CustomApplication> {

    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<CustomApplication>()

    override fun inject(application: CustomApplication)
}

--

@Module
class ApplicationModule {

    @Provides
    @Singleton
    fun provideContext(application: Application): Context {
        return application
    }
}

--

@Module
@Suppress("UNUSED")
abstract class ActivityBuilder {
    @ContributesAndroidInjector(modules = arrayOf(ActivitiesModule::class))
    internal abstract fun bindSplashActivity(): SplashActivity
}

--

@Singleton
class SplashPresenter @Inject constructor() {

    fun test() {
        Log.d("TAG", "this is a test")
    }
}

Now, instead of having the logged message harcoded, I would like to get it from string.xml, so I tried this:

@Singleton
class SplashPresenter @Inject constructor(private val context: Context) {

    fun test() {
        Log.d("TAG", context.getString(R.strings.test))
    }
}

But then I get this error:

Error:(7, 1) error: [dagger.android.AndroidInjector.inject(T)] android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method.

Could anyone tell me please how to inject the app context (or the resources) into the presenter?

Thanks.

You're using CustomApplication with Dagger in your ApplicationComponent , so that's what it knows about. It doesn't try to resolve types on its own, so Application is some class Dagger never heard about.

You can either add another @Provides / @Binds to bind CustomApplication > Application > Context or just go the direct way and change your code to require a CustomApplication instead of Application :

@Provides
@Singleton
fun provideContext(application: CustomApplication): Context {
    return application
}

  // ... or alternatively ...

@Provides
@Singleton
fun provideApplication(application: CustomApplication): Application {
    return application
}

@Provides
@Singleton
fun provideContext(application: Application): Context {
    return application
}

Either way your application can then be used as a Context .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM