Dagger 2 ContributesAndroidInjector为模块提供活动

时间:2017-12-06 09:43:56

标签: java android kotlin dagger-2 dagger

我正试图找出一种不那么模板化的方法来实现我所有应用活动中使用的ActivityModule。这是我目前的设置:

ActivityModule

@Module
class ActivityModule(private val activity: Activity) {

    @Provides @ActivityScope
    fun providesActivity(): Activity = activity

    @Provides @ActivityContext @ActivityScope
    fun providesContext(): Context = activity

    @Provides @ActivityContext @ActivityScope
    fun providesLayoutInflater(): LayoutInflater = activity.layoutInflater

    @Provides @ActivityContext @ActivityScope
    fun providesResources(): Resources = activity.resources

}

AppActivityModule (为AndroidInjectionModule提供活动)

@Module(subcomponents = [
        AppActivityModule.WelcomeActivityComponent::class
    ])
    internal abstract class AppActivityModule {

        @Binds 
        @IntoMap 
        @ActivityKey(WelcomeActivity::class)
        abstract fun bindWelcomeActivityInjectorFactory(builder: WelcomeActivityComponent.Builder): AndroidInjector.Factory<out Activity>

        @ActivityScope
        @Subcomponent(modules = [(ActivityModule::class)])
        interface WelcomeActivityComponent : AndroidInjector<WelcomeActivity> {
        @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<WelcomeActivity>() {
            abstract fun activityModule(myActivityModule: ActivityModule): AndroidInjector.Builder<WelcomeActivity>

            override fun seedInstance(instance: WelcomeActivity) {
                activityModule(ActivityModule(instance))
            }
        }
    }
}

我想要的AppActivityModule是:

@Module
internal abstract class AppActivityModule {
    @ContributesAndroidInjector(modules = [(ActivityModule::class)])
    abstract fun contributeWelcomeActivityInjector(): WelcomeActivity
}

但是,这很明显,给了我一个错误/di/AppActivityModule_ContributeWelcomeActivityInjector.java:29: error: @Subcomponent.Builder is missing setters for required modules or subcomponents: [...di.modules.ActivityModule]

我的问题是 - 是否有更少的样板方式来实现我想要做的事情?我知道@Bind@BindsInstance(来自this answer)但这似乎只有在我有一个每个模块的活动并绑定我不想要的具体活动类型时才有用case - 我希望ActivityModule能够处理所有活动。

1 个答案:

答案 0 :(得分:5)

最小化样板的一种方法是制作通用的ActivityModule,然后为每个Activity创建一个小的特定模块。原谅我的Kotlin缺乏经验,但这里有:

// Abstract class so you don't have to provide an instance
@Module
abstract class ActivityModule {

    // No need for ActivityScope: You're always binding to the same Activity, so
    // there's no reason to have Dagger save your Context instance in a Provider.
    @Binds @ActivityContext
    abstract fun providesContext(activity: Activity): Context

    // This doesn't *have* to be in a companion object, but that way
    // Android can do a static dispatch instead of a virtual method dispatch.
    // If you don't need that, just skip the constructor arguments and make these
    // normal methods and you'll be good to go.
    @Module
    companion object {
        @JvmStatic @Provides @ActivityContext
        fun providesLayoutInflater(activity: Activity): LayoutInflater = 
            activity.layoutInflater

        @JvmStatic @Provides @ActivityContext
        fun providesResources(activity: Activity): Resources = activity.resources
    }
}

你的模块:

@Module
internal abstract class AppActivityModule {

    @Module
    internal interface WelcomeActivityModule {
      // The component that @ContributesAndroidInjector generates will bind
      // your WelcomeActivity, but not your Activity. So just connect the two,
      // and suddenly you'll have access via injections of Activity.
      @Binds fun bindWelcomeActivity(activity: WelcomeActivity) : Activity
    }

    @ContributesAndroidInjector(
        modules = [ActivityModule::class, WelcomeActivityModule::class])
    abstract fun contributeWelcomeActivityInjector(): WelcomeActivity
}

请注意,虽然这适用于Activity,Service,BroadcastReceiver等,但您可能不希望它对Fragment这么快。这是因为dagger.android使用父片段处理片段层次结构,因此从子组件中您可以访问YourApplication,YourActivity,YourParentFragment和YourChildFragment及其所有组件。如果YourChildFragmentComponent中的某些内容依赖于一个不合格的片段,那么它是否真的需要YourParentFragment或YourChildFragment是不明确的。也就是说,这种设计确实对活动和某些碎片有意义,所以使用它(谨慎)是有道理的。