Dagger 2 Activity Context / ApplicationContext Modules

I am struggling with dagger 2 to understand how I can convey a context or other according to my needs. - First, I have an annotated @Singleton ApplicationModule, because it provides high-level objects, such as a webservice object, a model ... usually these objects are passed to ApplicationContext (since y need to live throughout the entire application life cycle)

@Singleton @dagger.Component(modules = { AppModule.class }) public interface AppComponent { void inject(MyApp application); Model model(); Context context();<--- should provide the application Context for the Object above (model) ... 

the implementation looks like this:

 @dagger.Module public class AppModule { private final Application app; public ApplModule(Application app) { this.app = app; } @Provides @Singleton Model provideModel(Bus bus) { return new Model(bus); } @Provides @Singleton Context provideApplicationContext() { return app.getApplicationContext(); } ... 
  • Secondly, I have an Activity Scope component in which I provide the current activity and various views that need context.

      @ActivityScope @Component( dependencies = AppComponent.class , modules = {ActivityModule.class} ) public interface ActivityComponent { void inject(MyActivity activity); Context context(); <---should provide the activity context MyView homeView(); <----takes a Context as a contructor parameter 

    @Module public class ActivityModule {private final Activity activity;

     public ActivityModule(Activity activity) { this.activity = activity; } @Provides @ActivityScope public Activity activity() { return activity; } @Provides @ActivityScope @Named("viewcontext") <----- if I removed this I get an error from Dagger public Context context() { return activity; } @Provides @ActivityScope MyView provideZeView(Bus bus, Model model) { <---- previously receiving the ApplicationContext as a parameter MyView v = new MyView(activity, bus, model); <---- must pass the activity otherwise passing the Context reveived is the ApplicationContext return v; } 

    So here are my questions:

  • I used scopes to have better “granularity” over what is passed in, and I still get applicationContext
  • If I delete @Named qulifier, I get an error
  • earlier Views created by another module with ActivityModule dependency but still getting ApplicationContext

Well, the thing is, of course, I’m missing something ... but I can’t understand that maybe I don’t understand how to use Scopes

+6
source share
2 answers

you can use such qualifiers. two separate files define the following:

 @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface ActivityContext { } @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface ApplicationContext { } 

then in your ActivityModule do the following:

 @Provides @ActivityScope @ActivityContext public Context context() { return activity; } 

as well as in your application:

  @Provides @Singleton @ApplicationContext Context provideApplicationContext() { return app.getApplicationContext(); } 

we now have a way to specify any type of context that we need, based on the @ApplicationContext and @ActivityContext classifiers.

so, for example, in your work you could do this:

  @Inject @ApplicationContext Context c; 

which will enter the application context.

and in the module you can do this, for example:

  @Provides @ActivityScope LoginPresenter provideLoginPresenter(@ActivityContext Context context) { return new LoginPresenter(context); } 

to provide an activity context. this is just an example.

+1
source

@Named is a requirement if you want to provide multiple objects of the same type from your module.

As for the second question regarding the transfer of the correct activity context, you need to have this in the ActivityComponent :

 Activity activity(); 
0
source

All Articles