Skip to content Skip to sidebar Skip to footer

How To Make Subcomponent Singleton In Dagger 2?

I want to make my subcomponent to be a singleton so that I can have Login Presenter to be a singleton as well. Is this possible? @Singleton @Component(modules = AppModule.class) pu

Solution 1:

@Subcomponents cannot be made @Singleton.

While the @Singleton spec is a little vague about it, "singleton" canonically means "one per application". Since a @Subcomponent is created via a factory method on a component, the only way in which your singleton-bound instances would be "one per application" would be if the singleton subcomponent were a child of a singleton component and its factory method were only ever invoked once per application. Enforcing that constraint is virtually impossible, so the pattern would just be a likely source of bugs.

Post a Comment for "How To Make Subcomponent Singleton In Dagger 2?"