I found a project on the githaba, which is presented as a benchmark for implementing MVP with Dagger 2. So, to my surprise, there are similar methods in the Activity module:

  @Provides @PerActivity MainMvpPresenter<MainMvpView> provideMainPresenter( MainPresenter<MainMvpView> presenter) { return presenter; } 

And since we are creating the activation component with OnCreate() :

  mActivityComponent = DaggerActivityComponent.builder() .activityModule(new ActivityModule(this)) .applicationComponent(((MvpApp) getApplication()).getComponent()) .build(); 

it turns out that when you turn the screen, the presenter will also die. After all, it is more reasonable to instantiate the presenter of the main activity to bind to the life of the application ? So why is the provider of the presenter located in the activation module?

    1 answer 1

    The reason is that Dagger 2 does not use a presenter to save the instance (saving the instance is required when the network request is completed and the presenter must accept the answer. The presenter should not die with activity). Of course, you can save AppComponent instance of the presenter in the AppComponent , but maybe when we leave this activity altogether and work on another activity, we must destroy the presenter. write presenter = null; This is additional logic. looks crooked. To solve this problem, retain fragments or the Moxy framework are used.