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?