我有一个包含多个嵌套片段的父片段。我想要一个对象,它会在父片段被创建时被创建并在父片段被销毁时被销毁(我猜这意味着父片段范围),以便我可以在子片段之间共享状态。
@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 中。
我做错了什么?