First you need to define a presenter module:
@Module
class SearchPresenterModule {
@NonNull
private final SearchContract.View mView;
SearchPresenterModule(@NonNull SearchContract.View view) {
this.mView = view;
}
@Provides
SearchContract.View provideSearchContractView() {
return mView;
}
}
Here's the example component:
@FragmentScoped
@Component(modules = SearchPresenterModule.class)
interface SearchComponent {
void inject(SearchActivity activity);
}
And inject your presenter:
@Inject
SearchPresenter mSearchPresenter;
DaggerSearchComponent.builder()
.searchPresenterModule(new SearchPresenterModule(searchFragment))
.build()
.inject(this);
Finally inject your presenter's constructor:
@Inject
SearchPresenter(@NonNull SearchContract.View view, @NonNull SearchRepository searchRepository) {
this.mView = view;
mView.setPresenter(this);
}
Extra: Here's fragmentscoped annotation:
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface FragmentScoped {
}
You can check my example repo for MVP + DAGGER2
https://github.com/savepopulation/wikilight