#StackBounty: #android #dagger-2 Dagger2 scope, instance per component

Bounty: 50

I’m looking for a quick confirmation about Dagger 2 scopes in Android.

In many resources online you will find that @ActivityScope and @FragmentScope are added to components that provide bindings for activities and fragments.

I would like to have some confirmation that this implies that there will be 1 instance for all activities / all fragments respectively.
That is, if, say, two activities use the same component for receiving dependencies from the same component annotated with scope ‘Activity’, both activities will receive the same instance (like singleton annotation would work).

So in that case having @ActivityScope and @FragmentScope annotations would only be useful to segregate between dependency lifetimes between activities versus fragments.
So if I would need a dependency object for which I need a separate instance in two activities, I should scope them explicitly (e.g. @LoginActivityScope).

Could you confirm that this assumption is correct?


Reading the docs about subcomponents, it confuses me a bit:

No subcomponent may be associated with the same scope as any ancestor
component, although two subcomponents that are not mutually reachable
can be associated with the same scope because there is no ambiguity
about where to store the scoped objects. (The two subcomponents
effectively have different scope instances even if they use the same
scope annotation.)

This would seem to assume that if you have multiple components using the same annotation, it does create a separate instance when the same scope annotation is used for different components.

I find it a bit unclear as to what a scope instance refers to. This actually refers to the binding?
Does this only apply to subcomponents?

Some clarification about scope vs dependency instances (bindings) would be very helpful.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.