-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to inject presenter using Dagger instead of @InjectPresenter? #100
Comments
@MasoodFallahpoor yes, it's. But then you should manually attach/detach view from prersenter and manage lifecycle of presenters. Also, you can use both Moxy and Dagger =) Then you should write some like this: @Inject
Presenter daggerPresenter;
@InjectPresenter
Presenter moxyPresenter;
@ProvidePresenter
Presenter providePresenter() {
return daggerPresetner;
}
@Override
public void onCreate(Bundle saveState) {
Component.get().inject(this);
super.onCreate();
.... Or @Inject
@InjectPresenter
Presenter presenter;
@ProvidePresenter
Presenter providePresenter() {
return presetner;
}
@Override
public void onCreate(Bundle saveState) {
Component.get().inject(this);
super.onCreate();
.... |
Thanks for your super quick response! |
@senneco In your second code sample, won't the presenter be injected by Dagger every time you call |
@alaershov you can move |
@A-Zaiats But then I won't be able to inject other dependencies into my activity, if it depends on something besides presenter. Maybe it would be better to do this: @InjectPresenter
MyPresenter mPresenter;
@ProvidePresenter
MyPresenter providePresenter() {
return mComponent.providePresenter();
}
@Override
public void onCreate(Bundle saveState) {
mComponent = getComponent();
mComponent.inject(this);
super.onCreate();
... It allows me to inject other dependencies to the activity, and does not reinject presenter on each recreate. |
I'm doing it like this: @InjectPresenter
MyPresenter mPresenter;
@Inject
Provider<MyPresenter> presenterProvider;
@ProvidePresenter
MyPresenter providePresenter() {
return presenterProvider.get();
}
@Override
public void onCreate(Bundle saveState) {
mComponent = getComponent();
mComponent.inject(this);
super.onCreate();
... |
@alaershov about this example: @Inject
@InjectPresenter
Presenter presenter;
@ProvidePresenter
Presenter providePresenter() {
return presetner;
}
@Override
public void onCreate(Bundle saveState) {
Component.get().inject(this);
super.onCreate();
.... If your Presenter was provided from Dagger with concrete Scope than there is only one instance of your Presenter and there is no cause for concern I think. |
@matzuk That's true, it's only a matter of taste. I find this example a bit confusing for beginners, but it's a nice puzzle for those who want to thoroughly understand the order of injections in Moxy+DI combo, as well as the essence of DI scopes. |
@alaershov with another DI framevork it looks more sweetly: @InjectPresenter
internal lateinit var presenter: ProfilePresenter
@ProvidePresenter
fun providePresenter(): ProfilePresenter {
return Toothpick.openScopes(DI.USER_SCOPE).getInstance(ProfilePresenter::class.java)
} |
@xanderblinov Toothpick is definitely more user-friendly than Dagger :) However, your example does not inject the Activity/Fragment itself, and the more complete example could look like this: private val scope: Scope by lazy { Toothpick.openScopes(DI.USER_SCOPE) }
@Inject
internal lateinit var someStuff: SomeStuff
@InjectPresenter
internal lateinit var presenter: ProfilePresenter
@ProvidePresenter
fun providePresenter(): ProfilePresenter {
return scope.getInstance(ProfilePresenter::class.java)
}
@Override
fun onCreate(...) {
Toothpick.inject(this, scope);
super.onCreate();
} |
@alaershov @xanderblinov |
Possible way of injection with Dagger (where Lazy is from dagger package
|
task #7 Moxy добавлен Ошибка исправлена( ProfilePresenter падает при вызове getProfile) рецепт исправления в Arello-Mobile/Moxy#100
in my case it doesn't worked out. I see in debugger, that dagger presenter was injected, but when calls providePresenter daggerPresetner is null. It seems like ProvidePresenter fulfills BEFORE @Inject fulfils. What could be cause of problem? thanks. Sorry for my english |
@danilNikolaenko1990 Hi there! Moxy has migrated to a new repository. This repository is deprecated and no longer maintained. You're welcome to open a new issue in the new repository, but if you only need some advice - welcome to our Telegram group! Feel free to ask any questions about Moxy there. |
thanks! |
Is it possible to inject the presenter using Dagger and @Inject instead of provided @InjectPresenter? From my experiments that's not possible.
The text was updated successfully, but these errors were encountered: