Component setup for Application and Activity injection

suggest change

A basic AppComponent that depends on a single AppModule to provide application-wide singleton objects.

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    void inject(App app);

    Context provideContext();

    Gson provideGson();
}

A module used with AppComponent. It provides the singleton objects, such as one Gson instance for the whole application.

@Module
public class AppModule {

    private final Application mApplication;

    public AppModule(Application application) {
        mApplication = application;
    }

    @Singleton
    @Provides
    Gson provideGson() {
        return new Gson();
    }

    @Singleton
    @Provides
    Context provideContext() {
        return mApplication;
    }
}

A subclass of Application that builds Dagger and holds the singleton component. Store the component yourself. Dagger cannot @Inject the AppComponent that is being built.

public class App extends Application {

    private AppComponent mAppComponent;

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

        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
        mAppComponent.inject(this);
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }
}

Now an activity scoped component that depends on the AppComponent to gain access to the singleton objects.

@ActivityScope
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
public interface MainActivityComponent {

    void inject(MainActivity activity);
}

A reusable ActivityModule provides activity-scoped objects, such as a FragmentManager. @ActivityScope is a @Scope annotation you declare. It is not a Dagger built-in. Each @Provides method needs @Provides, or Dagger ignores it.

@Module
public class ActivityModule {

    private final AppCompatActivity mActivity;

    public ActivityModule(AppCompatActivity activity) {
        mActivity = activity;
    }

    @ActivityScope
    @Provides
    public AppCompatActivity provideActivity() {
        return mActivity;
    }

    @ActivityScope
    @Provides
    public FragmentManager provideFragmentManager(AppCompatActivity activity) {
        return activity.getSupportFragmentManager();
    }
}

The activity can then be injected and share the same Gson as the rest of the app:

public class MainActivity extends AppCompatActivity {

    @Inject
    Gson mGson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaggerMainActivityComponent.builder()
                .appComponent(((App)getApplication()).getAppComponent())
                .activityModule(new ActivityModule(this))
                .build().inject(this);
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents