带有 Hilt 的 Dagger 从模块的适配器中注入 ActivityContext

Dagger with Hilt inject ActivityContext in adapter from module

我正在使用 dagger 和 hilt,我想将 @ActivityContext 从模块注入到适配器,但出现此错误 -

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/mnt/My Projects/app/build/generated/source/kapt/debug/app/myapp/MyApp_HiltComponents.java:156: error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.
  public abstract static class ApplicationC implements MyApp_GeneratedInjector,
                         ^
      @dagger.hilt.android.qualifiers.ActivityContext android.content.Context is injected at
          app.myapp.di.modules.activitiesModules.HomeActivityModule.provideAdapterFragmentState(context)
      app.myapp.ui.base.AdapterFragmentState is injected at
          app.myapp.ui.home.HomeActivity.adapterFragmentState
      app.myapp.ui.home.HomeActivity is injected at

这是我的 HomeActivityModule -

@Module
@InstallIn(ActivityRetainedComponent::class)
object HomeActivityModule {

    @Provides
    @ActivityRetainedScoped
    fun provideAdapterFragmentState(@ActivityContext context: Context): AdapterFragmentState {
        return AdapterFragmentState(context)
    }

}

这是我的适配器 -

@ActivityRetainedScoped
class AdapterFragmentState @Inject constructor(@ActivityContext context: Context)
    : FragmentStateAdapter(context as AppCompatActivity){

哪一部分是错误的?

@Module
@InstallIn(ActivityComponent::class)
object HomeActivityModule {

    @Provides
    @ActivityScoped
    fun provideAdapterFragmentState(@ActivityContext context: Context): AdapterFragmentState {
        return AdapterFragmentState(context)
    }

}

已编辑:

因为 ActivityRetainedComponent 在配置更改时存在,而 ActivityComponent 不会。

如果你想注入@ActivityContext,你的模块应该安装在ActivityComponent中,注入的对象必须是@ActivityScoped。