Dagger fragment scope

Viewed 290

I have a parent fragment with several nested fragments. I would like to have an object which would be created when the parent fragment is create and destroyed when parent fragment is destroyed (I guess it means parent Fragment scope), so that I can share state between child fragments.

@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() { ...}

The issue is that a new controller instance is created for each child/nested fragment.

The Controller is injected into the ViewModel of each child fragment.

What am I doing wrong?

Scopes

0 Answers
Related