While studying the library, dagger2 ran into a problem - I cannot understand how the inject annotation happens and works in this example.
for MainScreenPresenter.java . (an example is extremely simple and if you are familiar with dagger2 and using it in android, then I am sure you will have no difficulty in looking to say what's what)

You can see that the dependency is declared at the constructor level:

 @Inject public MainScreenPresenter(Retrofit retrofit, MainScreenContract.View mView) { this.retrofit = retrofit; this.mView = mView; } 

But in the only place where this class is used, the initiation procedure takes place in a single line.

 @Inject MainScreenPresenter mainPresenter; 

I don’t understand - where does the reference for Retrofit retrofit and MainScreenContract.View mView MainScreenPresenter come from at this place when initializing MainScreenPresenter if they are not advertised anywhere, and there is no connection to the presenter in the components and modules to provide the connection?

    1 answer 1

    Dagger looks for dependencies in your components, in this case Retrofit finds it in NetComponent , and MainScreenContract.View in MainScreenComponent . The same thing happens when adding parameters to the method that provides for example Retrofit

      @Provides @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .baseUrl(mBaseUrl) .client(okHttpClient) .build(); return retrofit; } 

    Gson and OkHttpClient dagger will try to find the available components.