How to make subcomponent singleton in dagger 2?

I want my subcomponent to be a single, so that I could also use a proxy server to log in. Is it possible?

@Singleton @Component(modules = AppModule.class) public interface AppComponent { LoginComponent getLoginComponent(); } @Singleton @Subcomponent(modules = LoginModule.class) public interface LoginComponent { } public class LoginComponent { @Singleton LoginPresenter getLoginPresenter(); } 
+1
source share
1 answer

@Subcomponent cannot be done by @Singleton .

While the @Singleton bit vague in this matter, "singleton" canonically means "one per app." Because @Subcomponent is created using the factory method on the component, the only way your single-window instances would be "one per application" would be if the single-component sub-component was a child of the single-element component and its factory was only ever called once for each application. Implementation of this restriction is almost impossible, so the template will be just a likely source of errors.

+2
source

Source: https://habr.com/ru/post/1271755/


All Articles