我试图找出一种不太花样的方法来实现在我的所有应用程序 Activity 中使用的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提供 Activity )
@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),但这似乎仅在我有每个模块的 Activity 并绑定(bind)我在这种情况下不想要的具体 Activity 类型时才起作用-我希望ActivityModule
与所有 Activity 一起工作。 最佳答案
最小化样板的一种方法是制作一个通用的ActivityModule,然后为每个Activity创建一个小的特定模块。
// 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.
companion object {
@Provides @ActivityContext
fun providesLayoutInflater(activity: Activity): LayoutInflater =
activity.layoutInflater
@Provides @ActivityContext
fun providesResources(activity: Activity): Resources = activity.resources
}
}
(在早于2.26的Dagger版本中,您可能需要像在this previous revision中一样在同伴对象上添加@Module,并在@Provides方法上添加@JvmStatic。由于Dagger 2.26于2020年1月发布,所以不需要这样做。谢谢arekolek!)
和您的模块:
@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使用父 fragment 处理 fragment 层次结构,因此您可以从子组件中访问YourApplication,YourActivity,YourParentFragment和YourChildFragment及其所有组件。如果YourChildFragmentComponent中的某些内容依赖于不合格的Fragment,则它是否真正想要YourParentFragment或YourChildFragment将会是模棱两可的。也就是说,这种设计对于“Activity ”和某些“fragment ”确实有意义,因此(谨慎地)使用它是有意义的。
编辑:此处
@ActivityContext
在做什么?@ActivityContext
是您要定义的qualifier annotation,可用于区分Dagger和其他DI框架中相同类型的绑定(bind),大概是@ApplicationContext Context
与@ActivityContext Context
。可以尝试使用它,但是我强烈建议您保留它,避免绑定(bind)不合格的上下文:应用程序和 Activity 上下文可能有所不同,尤其是在多屏幕或自动环境中,并且为了获得正确的资源和数据,应该准确地说明您使用的是什么。您可以使用this one为例。