1

I'm trying to use dagger 2 on my app. The app has only MainActivity and different fragment which navigation is implemented by the navigation component. I want to have different subcomponents throughout the app such as AuthSubcomponent, HomeSubcomponent. What I want to achieve is:

@Singleton (root component) with MainActivity

@AuthSubcomponent (subcomponent of @Singleton) with TitleFragment,SignUpFragment,LoginFragment, VerificationFragment

@HomeSubcomponent(subcomponent of @Singleton) with HomeFragment, ProfileFragment

The problem comes when I have to glue different fragments together and have them being part of the same subcomponent. For example, TitleFragment, SignUpFragment, LoginFragment, and VerificationFragment all have to be part of AuthSubcomponent. However, when I write the subcomponent by following the dagger2 documentation, only one fragment can be part of it.

Example:

Subcomponent

@FiScope
@Subcomponent
interface AuthSubcomponent : AndroidInjector<TitleFragment> {

    @Subcomponent.Factory
    interface Factory: AndroidInjector.Factory<TitleFragment>

}

ActivityModule

@Module(subcomponents = [AuthSubcomponent::class])
abstract class ActivityModule {

    @ContributesAndroidInjector
    abstract fun contributeMainActivity(): MainActivity

    @ContributesAndroidInjector
    abstract fun contributeTitleFragment(): TitleFragment
}

AppComponent

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, ActivityModule::class])
interface AppComponent {

    @Component.Factory
    interface Factory{
        fun create(@BindsInstance application: Application): AppComponent
    }
    fun inject(app: BaseApplication)
    fun authSubcomponent(): AuthSubcomponent.Factory
}

At this point, how can I add SignUpFragment, LoginFragment, and VerificationFragment to the AuthSubcomponent?

1 Answers1

0

you should have another AuthFragmentModule and install it on AuthSubcomponent,

@FiScope
@Subcomponent(modules =[AuthFragmentModule::class])
interface AuthSubcomponent : AndroidInjector<TitleFragment> {

    @Subcomponent.Factory
    interface Factory: AndroidInjector.Factory<TitleFragment>

}

inside the AuthFragmentModule you should add

@Moudle 
class AuthFragmentModule{
    //add the fragments LoginFragment and singupFragment here 
    @FiScope
    @ContributesAndroidInjector
    abstract fun contributeTitleFragment(): TitleFragment
}

with each subcomponent you should have a custom scope

Fahad Alotaibi
  • 416
  • 3
  • 9
  • 1
    So how can I add SignUpFragment, LoginFragment, and VerificationFragment to AuthSubcomponent? Should I add to AuthSubcomponentModule `@FiScope @ContributesAndroidInjector abstract fun contributeLoginFragment(): LoginFragment` and the other fragments? – Marco Galdi Mar 16 '20 at 09:02
  • what do you mean by AuthSubcomponent what is it for? – Fahad Alotaibi Mar 16 '20 at 09:06
  • 1
    I want AuthSubcomponent to encapsulate Title,Signup,Login, and Verification, and all be part of the same @FiScope. – Marco Galdi Mar 16 '20 at 09:17