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?