I probably missed something, but I thought Scopes like @Singleton are used to define \"scoped lifecycles\".
I use Dagger 2 in an Android app (but I don\'t think the
You can do the following to define a real singleton for multiple components. I am assuming @ApplicationScoped and @ActivityScoped to be the different scopes.
@Module public class MailModule {
@Provides @ApplicationScoped
public AccountManager providesAccountManager() {
return new AccountManager();
}
@Provides @ApplicationScoped
public MailProvider providesMailProvider(AccountManager accountManager) {
return new MailProvider(accountManager);
}
}
Then a MailComponent can be defined for the MailModule. The LoginComponent and MenuComponent can depend on the MailComponent.
@ApplicationScoped
@Component(modules = MailModule.class)
public interface MailComponent {
MailProvider mailProvider();
AccountManager accountManager();
}
@ActivityScoped
@Component(dependencies = MailComponent.class)
public interface LoginComponent {
LoginPresenter presenter();
}
@ActivityScoped
@Component(dependencies = MailComponent.class)
public interface MenuComponent {
MenuPresenter presenter();
}
The MailComponent can be initialized as shown below and can be used in MenuComponent and LoginComponent again shown below.
MailComponent mailComponent = DaggerMailComponent.builder().build();
DaggerMenuComponent.builder().mailComponent(mailComponent).build();
DaggerLoginComponent.builder().mailComponent(mailComponent).build()