使用Dagger Hilt将片段/活动绑定到MVP View合约

时间:2020-06-17 11:32:36

标签: dagger-hilt

我目前正在尝试从dagger-android迁移到Hilt。以前,我使用@Bind将片段和活动的实例绑定到相应的MVP View合约。有没有办法对Hilt做同样的事情?似乎无法注入特定的片段和活动?

1 个答案:

答案 0 :(得分:0)

我们需要一些代码来给出正确的答案,但我认为您需要类似的东西

@InstallIn(ActivityComponent::class)
@Module
abstract class MyScreenModule {

    @ActivityScoped
    @Binds
    abstract fun bindMyScreenFragment(impl: ScreenFragment): MyScreenContract.View


    @ActivityScoped
    @Binds
    abstract fun bindMyScreenPresenter(impl: ScreenPresenter): MyScreenContract.Presenter

}

别忘了:

  • 在Fragment和Presenter构造函数中添加“ @Inject”
  • 将@AndroidEntryPoint添加到MyScreenActivity和MyScreenFragment

注意::请考虑是否应将Presenter绑定到Fragment生命周期。在这种情况下,presenter绑定应将其自己的模块标记为 @InstallIn(FragmentComponent :: class),绑定的作用域应为@FragmentScoped