I'm using Dagger 2 with simple MVP pattern. I have a @PerApp and @PerActivity scopes. I'm injecting presenters dependencies using constructor injection, which makes those presenters "injectable" (I don't need to write provides methods in activity module). Code fragments:
PerApp:
// AppComponent
@PerApp
@Component(modules = {AppModule.class, DataModule.class, NetworkModule.class})
public interface AppComponent {
    LoginComponent plus(LoginModule loginModule);
    MainComponent plus(MainModule mainModule);
}
// Example @PerApp module
@Module
public class NetworkModule {
    @Provides
    @PerApp
    Retrofit providesRetrofit(){
         ...
    }
}
PerActivity:
// LoginModule
@Module
public class LoginModule {
    private final LoginActivity mLoginActivity;
    public LoginModule(LoginActivity loginActivity) {
        mLoginActivity = loginActivity;
    }
    @Provides
    @PerActivity
    Context providesContext() {
        return mLoginActivity;
    }
}
// LoginComponent
@PerActivity
@Subcomponent(
        modules = LoginModule.class
)
public interface LoginComponent {
    void inject(LoginActivity loginActivity);
}
Activity:
public class LoginActivity {
    @Inject LoginPresenter mPresenter;
}
Presenter:
public class LoginPresenter {
    @Inject
    public LoginPresenter(Retrofit retrofit) {
        ...
    }
}
It's working great. My question is: What will be the scope of provided LoginPresenter? Is it gonna be the same as LoginActivity? Should I annotate the presenter's constructor with @PerActivity or something?
I've ran some tests to check by myself how exactly scoping works with constructor injection and here are my results.
LoginPresenter into LoginActivity:LoginComponent - @PerActivity scope (code is exactly the same as in my first post).
I've tried to inject presenter into 2 variables:
public class LoginActivity {
    @Inject LoginPresenter A;
    @Inject LoginPresenter B;
}
LoginPresenter annotated with:
A and B are differentA and B are the sameA and B are the sameLoginPresenter into LoginActivity and MainActivity:LoginComponent, MainComponent - @PerActivity scope (code is exactly the same as in my first post).
I've tried to inject presenter into 2 different activities:
public class LoginActivity {
    @Inject LoginPresenter A;
}
public class MainActivity {
    @Inject LoginPresenter B;
}
LoginPresenter annotated with:
A and B are differentA and B are differentA and B are the sameIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With