匕首片段范围

时间:2021-02-01 11:52:04

标签: android android-fragments dagger-2

我有一个包含多个嵌套片段的父片段。我想要一个对象,它会在父片段被创建时被创建并在父片段被销毁时被销毁(我猜这意味着父片段范围),以便我可以在子片段之间共享状态。

@Module
internal abstract class Module {

    @FragmentScope
    @ContributesAndroidInjector(modules = [ParentFragmentModule::class])
    abstract fun getParentFragment(): ParentFragment
}
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope
@Module
class ParentFragmentModule {

    @FragmentScope
    @Provides
    fun provideSharedController() = SharedController()
}
class SharedController @Inject constructor() { ...}

问题是为每个子/嵌套片段创建了一个新的控制器实例。

Controller 被注入到每个子片段的 ViewModel 中。

我做错了什么?

Scopes

0 个答案:

没有答案