Link to activity inside the module

How to use the new AndroidInjector.inject and still be able to provide an Activity instance inside the activity module? Dagger docs do not make it clear how to do this.

The use case is as follows: I have an activity module that provides the presenter with my activity, but the presenter needs a link to Activity. I used to have something like

 @Inject Presenter presenter; public onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((CustomApplication) getApplicationContext()) .getAppComponent() .plus(new ActivityModule(this)); } 

Can someone point me to a sample that uses AndroidInjector.inject(this) instead and allow the Activity link inside the Dagger 2 module?

+3
android dagger-2
source share
1 answer

Flag Dagger 2 Github issue 615

An instance of your activity is automatically provided, just pass it as a parameter in the methods of your module.

Example:

 @Provides @ActivityScope public providePresenter(ActivityA activity) { return new PresenterA(activity); } 

Now you can abstract from simple modules. Your presenter can also be entered into the constructor.

This actually cuts out a lot of code from all of my modules.

+4
source share

All Articles