简体   繁体   English

无法从Dagger(Android)中的片段组件访问活动组件

[英]Unable to access Activity Component from Fragment Component in Dagger(Android)

I am trying to create dependent components for Activity and Fragment . 我正在尝试为ActivityFragment创建依赖组件。 Here's my Activity Component : 这是我的活动组件

@Component(modules = [ActivityModule::class],
        dependencies = [AppComponent::class])

interface ActivityComponent{
    fun inject(activity: Activity)

    @Component.Builder
    interface Builder{
        fun appComponent(component: AppComponent): Builder
        fun activityModule(activityModule: ActivityModule): Builder
        fun build(): ActivityComponent
    }
}

@Module
class ActivityModule(private val activity: Activity) {
    @Provides
    fun provideActivity() = activity
}

Fragment Component: 片段组件:

@Component(modules = [FragmentModule::class],
        dependencies = [ActivityComponent::class])

interface FragmentComponent{
    fun inject(fragment: Fragment)

    @Component.Builder
    interface Builder{
        fun activityComponent(activityComponent: ActivityComponent): Builder
        fun fragmentModule(fragmentModule: fragmentModule): Builder
        fun build(): FragmentComponent
    }
}

@Module
class FragmentModule(private val fragment: Fragment) {
    @Provides
    fun provideFragment() = fragment
}

I am getting a compile error in places where I am trying to access activity. 我在尝试访问活动的地方遇到编译错误。

Activity cannot be provided without an @Provides-annotated method. 如果没有@Provides注释的方法,则无法提供活动。

It's my understanding that since Activity has already been injected in the ActivityComponent , and Fragment is simply adding itself to the ActivityComponent , it should get all the dependencies provided by ActivityComponent and AppComponent 's modules. 这是我的理解,既然Activity已经在注入ActivityComponent ,并且Fragment被简单地将自身添加到ActivityComponent ,它应该得到所提供的所有依赖ActivityComponentAppComponent的模块。 I see this happening in ActivityComponent , where I get dependencies from AppComponent automatically. 我在ActivityComponent看到了这种情况,我从AppComponent自动从AppComponent获取依赖AppComponent

Update : If I add the following code to the FragmentModule , I am able to get the code compiled: 更新 :如果将以下代码添加到FragmentModule ,则可以编译该代码:

@Provides
fun provideActivity() = fragment.context as Activity

But this is not making sense to me, as I was hoping to get the pre-injected Activity from ActivityComponent 但是,这不是决策意识给我,因为我希望得到的预注入ActivityActivityComponent

I realized that parent components should always declare the dependencies they want to expose. 我意识到父组件应始终声明它们要公开的依赖项。 Otherwise dependent components cannot access them just by declaring them as dependents. 否则,依赖组件不能仅通过将它们声明为依赖项来访问它们。 Note that transitive dependencies don't work either. 请注意,传递依赖项也不起作用。 So AppComponent has to declare a dependency for ActivityComponent to use it - but ActivityComponent has to re-declare the same dependency if FragmentComponent needs it. 因此, AppComponent必须声明一个依赖项,以便ActivityComponent可以使用它-但是,如果FragmentComponent需要它,则ActivityComponent必须重新声明相同的依赖项。

interface ActivityComponent{
    fun inject(activity: Activity)

    @Component.Builder
    interface Builder{
        fun appComponent(component: AppComponent): Builder
        fun activityModule(activityModule: ActivityModule): Builder
        fun build(): ActivityComponent
    }
    var activity: Activity
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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