The lost droid and the magic Dagger: an intro to dependency injection for Android.

cxr
cxr
Feb 14, 2017 · 6 min read
Image for post
Image for post

Definitions and advantages

Dagger 2

Concepts

@Module
class ApplicationModule {

@Provides
MovieDbHelper provideMovieDbHelper() {
return new MovieDbHelperImpl();
}
}
@Module
class ApplicationModule {

private Context context;

ApplicationModule(Context context) {
this.context = context;
}

@Provides
Context providesContext() {
return context;
}

@Provides
@Singleton
MovieDbHelper provideMovieDbHelper(Context context) {
return new MovieDbHelperImpl(context);
}
}
class MovieDbHelperImpl {    @Inject
MovieDbHelperImpl(Context context) {
this.context = context;
}
[...]
}
class SearchMoviesActivity extends AppCompatActivity {

@Inject SearchMoviesPresenter presenter;
[...]
}
class SearchMoviesActivity extends AppCompatActivity {

SearchMoviesPresenter presenter;

@Inject
void onInject(SearchMoviesPresenter presenter) {
this.presenter = presenter;
}
[...]
}
@Component(modules = {ApplicationModule.class})
interface ApplicationComponent {
void inject(SearchMoviesActivity searchMoviesActivity);}
public class SearchMoviesActivity extends AppCompatActivity {

private ApplicationComponent applicationComponent;

@Override
public void onCreate() {
super.onCreate();

applicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(
new ApplicationModule(getApplicationContext()))
.build();

applicationComponent.inject(this);
}
[...]
}

Simple sample setup

dependencies {
compile "com.google.dagger:dagger:2.9"
annotationProcessor "com.google.dagger:dagger-compiler:2.9"
provided "javax.annotation:jsr250-api:1.0"
}
@Module
public class ApplicationModule {

private Application app;

public ApplicationModule(Application app) {
this.app = app;
}

@Provides
@Singleton
Context providesContext() {
return app;
}

}
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {

void inject(MainMenuActivity mainMenuActivity);

void inject(PlayGameActivity playGameActivity);
}
public class MyApplication extends Application {

private ApplicationComponent applicationComponent;

@Override
public void onCreate() {
super.onCreate();

applicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(
new ApplicationModule(getApplicationContext()))
.build();
}

public ApplicationComponent getComponent() {
return applicationComponent;
}
}
<application
android:name=".application.MyApplication"
[...]
public abstract class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(((MyApplication) getApplication()).getComponent());
}

/**
* Implement and call component.inject(this) for dependency
* injection of this Activity.
*/
protected abstract void inject(ApplicationComponent component);
}
public class MainMenuActivity extends BaseActivity {

@Inject MainMenuPresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[...]
});

@Override
protected void inject(ApplicationComponent component) {
component.inject(this);
}
[...]
}
public class MainMenuPresenter {

@Inject
MainMenuPresenter() {
[...]
}
}

Conclusion

RockNNull

A blog about technology and programming.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store