So here are the things I know from the doc
- Dagger Android under the hood is creating subcomponent for each Activity annotated with
ContributesAndroidInjector - You can apply custom scope to the method where
ContributesAndroidInjectoris annotated to - If two sibling subcomponents have the same scope, they will still have different scope instances
- If an Activity is in a subcomponent, it can have its own subcomponent which can contain Fragments. Those Fragments will share the scoped instances the Activity has.
Now my question is: How to have one Activity be a subcomponent of another activity using Dagger Android?
I want to do this because I want to achieve things like @UserScope/@SessionScope.
From this I know that I can do it with just Dagger not Dagger Android. But with Dagger Android, you can only have the Application (which is the AndroidInjector) to inject Activity. You can not have an Activity used as a holder or host of the parent subcomponent to inject another Activity.
Am I understanding it correctly?
05/14/2018 Update:
I ended up getting rid of Dagger Android. So no more ContributesAndroidInjector, just pure Dagger. And to inject Activity/Fragment, I use the way that's recommended here. It will be something like this:
class MyActivity : AppCompatActivity() {
private val factory: ViewModelProvider.Factory = Injector.myCustomScope().factory()
}
And we are trying to make sure the factory is the only thing that Activity/Fragment needs.
So far it's been great.