Java – Activity Context cannot be provided using Dagger 2

Activity Context cannot be provided using Dagger 2… here is a solution to the problem.

Activity Context cannot be provided using Dagger 2


I’m new to Dagger 2 and I’m trying to provide an Activity Context for a class without success. I searched a lot but didn’t find any suitable answers.

I can provide application context. But I also need to provide an Activity Context, but I don’t know of any good way to implement it.


need to clarify that I’m using Dagger as an Android dependency.

def dagger_version = "2.24"
implementation "$dagger_version"
annotationProcessor "$dagger_version"
implementation "$dagger_version"
implementation "$dagger_version"
annotationProcessor "$dagger_version"

I also only have one AppComponent with the following code:

        modules = {
public interface AppComponent extends AndroidInjector<BaseApplication> {

SessionManager sessionManager();

    interface Builder{

        Builder application(Application application);

AppComponent build();

Except that each of my activities has a module, I didn’t find a way to inject the activity context for AppComponent or from ActivityModule.

What is the right thing to do?


I finally found the right way.

First I created a module for the class I wanted to provide

public class AlertsModule {

    static Alerts provideAlerts(Activity activity){
        return new Alerts(activity);


Then I go to the ActivityModules I want to inject into that custom class and do a similar binding

public abstract class MainActivityModule {


    abstract Activity providesActivity(MainActivity activity);



Finally, I

included CustomClassModule in my ActivityBuildersModule, where I used @ContributesAndroidInjector to provide my activities.

            modules = {
    abstract MainActivity contributeMainActivity();


You can bind a (bind) activity instance in the same way as the current bind (bind) application instance by using the @Component.Builder or @Component.Factory

An example implementation looks like this:

interface ActivitySubcomponent {

    interface Factory {
        ActivitySubcomponent create(@BindsInstance MyActivity activity)
@Module(subcomponents = [ActivitySubcomponent.class])
class ApplicationModule {
public class MyActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        ((MyApplication) getApplication())

Related Problems and Solutions