@Singleton
, in Dagger 1.x, acts differently than you might think. The JSR-330 spec definition in the @Singleton
javadoc is "one per graph" and that is how Dagger interprets it.
So if you have something that is marked as @Singleton
, and it is materialized in your application graph (as opposed to a shorter-lifetime graph), then you get one instance per application.
If you have an item annotated @Singleton
that's in the modules you use to configure your activity graph (i.e., that is obtained from the part of a graph specified by a module used in the plus() operation) then you will get one-per-activity-graph.
If you need something to be once-per-application, you need to make sure it gets created as a part of the application graph. You can do this in one of two ways. Either explicitly provide it with an @Provides method from your application module(s), or you can list it as a one of the classes in @Module(injects=...) in an application module.
(If you do not mark it with @Singleton
than you will get one per injection site.)
So remember, the graph created by plus() is seen as a separate graph that points to the graph from which it was spawned, and wraps it, can access instances within it, but is not the same graph.
Note - Dagger 2.x improves this, and supports custom scoping annotations, though the mechanism is similar, with one graph (component) per scope annotation, with a parent/child relationship between graphs of wider/narrower lifetimes
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…