I want to explain some key points related to Dagger 2 from my understanding.
The main participants:
A βcomponentβ is a bridge between the modules and the places where the injection takes place.
A βmoduleβ is the place where we declare our objects to be introduced.
The scale is similar to the lifetime of a related injection history.
How does it work?
-Declare component with scope ("Singleton").
-Define the objects that can insert the provided objects into the component module.
void inject (BaseFragment baseFragment);
******* Provide the provided objects in the component module to subcomponents ****
DbHelper dbHelper ();
-Declare and provides objects that need to be inserted through the component.
Example:
@Singleton @Component(modules = { ApplicationModule.class, NetworkModule.class }) public interface ApplicationComponent { void inject(BaseActivity baseActivity); DbHelper dbHelper(); } @PerService @Component(dependencies = ApplicationComponent.class, modules = ServiceModule.class) public interface ServiceComponent { void inject(SyncService service); }
//SyncService.java
@Inject DbHelper dbHelper; (even Singleton scoped) private void setupInjector() { ServiceComponent mServiceComponent = DaggerServiceComponent.builder() .applicationComponent(getApplicationComponent()) .serviceModule(new ServiceModule(this)) .build(); mServiceComponent.inject(this); }
ok then ... 
You can embed objects with a scope without cloud and (Singleton and PerService) in your SyncService.class
source share