/** Dagger 2 injection in non Activity Java class **/
/** You should generally use constructor injection whenever possible. The call to component.inject(myObject) is mostly to be used for objects which you can not instantiate yourself (like activities or fragments).
Constructor injection is basically what you already did: **/
private class MyManager {
private SharedPreferencesManager manager;
@Inject
MyManager(SharedPreferencesManager manager){
this.manager = manager;
}
}
/**
Dagger will create the object for you and pass in your SharedPreferencesManager
There is no need to call init or something similar.
**/
/** The real question is how to obtain an object of MyManager. To do so, again, dagger will handle it for you.**/
/** By annotating the constructor with @Inject you tell dagger how it can create an object of that type. To use it, just inject it or declare it as a dependency.**/
private class MyActivity extends Activity {
@Inject
MyManager manager;
public void onCreate(Bundle savedState){
component.inject(this); //
}
}
/** Or just add a getter to a component (as long as SharedPreferenceManager can be provided, MyManager can also be instantiated):**/
@Component(dependencies = SharedPreferenceManagerProvidingComponent.class)
public interface MyComponent {
MyManager getMyManager();
}
/** How to inject pojo dependencies using dagger 2? List or Primitive Type Dependency**/
/** Below is the problem **/
public class MySimpleClass {
private List<String> mDependency;
public MySimpleClass (List<String> dependency) {
mDependency = dependency;
}
}
/**And I'm trying to have it created using dependency injection using dagger 2. Right now I have a simple module and component for it:**/
@Module
public class MySimpleClassModule {
@Provides
MySimpleClass provideMySimpleClass(List<String> dependency) {
return new MySimpleClass(dependency);
}
}
@Component(modules={MySimpleClassModule.class})
public interface MySimpleClassComponent {
}
/**But I'm not sure how can I inject the List<String> dependency every time I need to create a new instance of MySimpleClass. In the above scenario, it seems I would actually need to add List<String> to the constructor of MySimpleClassModule and have a new instance of that module every time I need a new instance of MySimpleClass with a new List<String>. Is that correct? It seems like a lot of overhead in this particular case.**/
/*****
Solution:
********/
/** No, it is not.**/
/** I assume you got a compilation error with Dagger since from question it is not clear if you already have a module that provides this list of strings.
To fix that one you can simply:
**/
@Module
public class MySimpleClassModule {
@Provides
List<String> provideListDependency() {
return Arrays.asList("One", "Two");
}
@Provides
MySimpleClass provideMySimpleClass(List<String> dependency) { ///<--- This list dependency come from above method
return new MySimpleClass(dependency);
}
}
/**If you think that providing this list should be part of a different module you can move it. The main thing, that Dagger during compilation was able to find how to get this dependency.**/