Dagger02: Inject Activities with @ContributesAndroidInjector

  • Components acts as Services.
  • Activities/Fragments acts as Clients.
  • Dagger Module: They are a place for dependencies to live so that we can add them to the components.

1. Create a ActivityBuildersModule class. This is the class where the DI of the Activity we want would happen. In this case it is AuthActivity.

import dagger.Module;
import dagger.android.ContributesAndroidInjector;

@Module
public abstract class ActivityBuildersModule {
    @ContributesAndroidInjector
    abstract AuthActivity contributeAuthActivity();
}


2. Add this module into the AppComponent class module section.

@Component(
        modules = {
                AndroidSupportInjectionModule.class,
                ActivityBuildersModule.class
        }
)

Now we can basically inject stuff into the AuthActivity.

3. Change the Activity to extend from DaggerCompatActivity.

Comments

Popular posts from this blog

What happened to the blog ?

Why I love to be a programmer ?