Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Created April 5, 2019 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/49de015e3dbba3f930ea291ce903694d to your computer and use it in GitHub Desktop.
Save mitchtabian/49de015e3dbba3f930ea291ce903694d to your computer and use it in GitHub Desktop.
Example of making use of the Application context using @Component.Builder in AppComponent class (new way)
@Singleton
@Component(
modules = {
AndroidInjectionModule.class,
AppModule.class,
ActivityBuildersModule.class
})
public interface AppComponent extends AndroidInjector<BaseApplication> {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
}
public void init(BaseApplication app){
appComponent = DaggerAppComponent.builder()
.application(app)
.build();
appComponent.inject(app);
}
@Module
public class AppModule {
@Singleton
@Provides
Drawable getDrawable(Application application){ // application is available here
return ContextCompat.getDrawable(application, R.drawable.ic_launcher_background);
}
}
@Inject
Drawable getDrawable;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getDrawable == null){
Log.d(TAG, "onCreate: drawable is null.");
}
else {
Log.d(TAG, "onCreate: drawable is NOT null.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment