I some time testing some things with Dagger, after overseeing the series of articles here: http://antonioleiva.com/dependency-injection-android-dagger-part-1/, went back for more information, I saw some good examples like these: https://github.com/adennie/fb-android-dagger, most of which focuses on injecting dependencies on Activity
, Fragment
, Service
and related. I'm wanting to do a similar thing with RoboGuice.
In RoboGuice
public class Utils {
@InjectResource(R.string.hello_world) private String hello;
public void showLog(){
System.out.println("String from injected resource : " + hello);
}
}
In Dagger
public class Utils {
@Inject
Resources mResource;
public void showLog() {
System.out.println( "String from injected resource : " + this.mResource.getString( R.string.hello_world ) );
}
}
I created an module in my Application:
@Module( injects = {Utils.class }, complete = false, library = true )
public class ResourceModule {
Context mContext;
public ResourceModule ( final Context mContext ) {
this.mContext = mContext;
}
@Provides
@Singleton
Resources provideResources() {
return this.mContext.getResources();
}
}
Tried this in my Activity
Utils mUtils = new Utils();
mUtils.showLog();
But I'm getting NullPointerException
. Someone already did something similar?
Defining dependencies (object consumers) You use the @Inject annotation to define a dependency. If you annotate a constructor with @Inject , Dagger 2 can also use an instance of this object to fulfill dependencies. This was done to avoid the definition of lots of @Provides methods for these objects.
onCreate() , an activity attaches fragments that might want to access activity bindings. When using fragments, inject Dagger in the fragment's onAttach() method. In this case, it can be done before or after calling super. onAttach() .
inject. Inject annotation to identify which constructors and fields it is interested in. Use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.
The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.
You have to inject the object using inject
method.
Assuming your ObjectGraph
is initialized in the App
class which is a subclass of the Application
and exposes public inject
method with implementation like below:
public void inject(Object object) {
mObjectGraph.inject(object);
}
After creating the Utils
instance you have to inject its dependencies.
Utils mUtils = new Utils();
((App) getApplication).inject(mUtils);
mUtils.showLog();
Edit:
You can also use constructor injection (no need for passing the object to ObjectGraph
)
public class Utils {
private final Resources mResource;
@Inject
public Utils(Resources resources) {
mResources = resources;
}
public void showLog() {
System.out.println( "String from injected resource : " + this.mResource.getString( R.string.hello_world ) );
}
}
With constructor injection the object must be created by the ObjectGraph
Utils mUtils = mObjectGraph.get(Utils.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With