Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger activity graph and separated modules

I have an Android application and I'm trying to use dagger. I decided to use a global graph + activity graphs. I have three modules:

AndroidModule

@Module(library = true)
public class AndroidModule {

  private final MApplication mApplication;

  public AndroidModule(MyApplication application) {
    mApplication = application;
  }

  @Provides @Singleton @ForApplication Context provideApplicationContext() {
    return mApplication;
  }
}

MyAppModules

@Module(
    injects = {
            MainActivity.class,
            AddFragment.class,
            AddMapFragment.class,
            MyActivity.class,
            ListFragment.class,
            MyMapFragment.class,
            RetainFragment.class,
            SingleActivity.class,
            UserActivity.class,
            UserLoginFragment.class,
            UserProfileActivity.class,
            UserProfileFragment.class,
            UserRegisterFragment.class,
            WelcomeFragment.class
    },
    complete = false,
    library = true,
    includes = ActivityModule.class
)
public class MyAppModule {

  private final Bus mBus = new Bus();

  @Provides @Singleton Bus provideBus() {
    return mBus;
  }

  @Provides @Singleton
  RequestQueue provideRequestQueue(@ForApplication Context context) {
    return Volley.newRequestQueue(context);
  }

  @Provides @Singleton Api provideApi(RequestQueue queue) {
    return new AppApi(queue);
  }

  @Provides Logger provideLogger() {
    return new MyLogger();
  }
}

ActivityModule

@Module(
    complete = false,
    library = true
)
public class ActivityModule {

  private BaseActivity mActivity;
  public ActivityModule() {}

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

  @Provides @Singleton @ForActivity Context provideActivityContext() {
    return mActivity;
  }

  @Provides @Singleton LocationClientFactory provideLocationClientFactory() {
    return new LocationClientFactory(mActivity);
  }
}

I had to use include on MyAppModule for the ActivityModule, without it, the application crashed saying it could find a constructor on Context and couldn't inject the Activity's Context.

Is the graph configured correctly? I'm adding the modules to the graph in the Application and in a BaseActivity and I thought it was enough and I didn't need the include, but I'm not sure I completly understand the configuration process of this.

Edit

Like this, dagger gives no errors but if I try to inject an Activity Context, at runtime it's null.

like image 223
alex Avatar asked Oct 04 '22 21:10

alex


1 Answers

The solution, if anyone is interested, was to remove

injects = {
        MainActivity.class,
        AddFragment.class,
        AddMapFragment.class,
        MyActivity.class,
        ListFragment.class,
        MyMapFragment.class,
        RetainFragment.class,
        SingleActivity.class,
        UserActivity.class,
        UserLoginFragment.class,
        UserProfileActivity.class,
        UserProfileFragment.class,
        UserRegisterFragment.class,
        WelcomeFragment.class
}

from MyAppModule and put it in ActivityModule.

like image 56
alex Avatar answered Oct 07 '22 19:10

alex