Arello-Mobile / Moxy

Moxy is MVP library for Android

Home Page:https://github.com/Arello-Mobile/Moxy/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect behavior with FragmentPagerAdapter and Dagger

bitvale opened this issue · comments

Hi! I have an Activity with view pager. In view pager I show one fragment (one - because it's an example) with FragmentPagerAdapter. In this one fragment I inject presenter using Dagger. In phone developer settings I enable Not save activity state option. Now when I press home button activity and fragment are destroyed, but presenter not and when I came back to the app two instance of presenter is created: one - the old and one - new is injected. But if I add mvpDelegate.onDestroy() in fragments onDestroy the presenter is destroyed, new instance is injected and there is no old presenter instance.

There is a lot of code to paste. I've created a sample project to show this issue. Run it and see log after destroy and resume. But before go to settings and enable Not save activity state option.

Link to project: https://github.com/bitvale/MoxyIssue

Hi @bitvale!
This is not a library issue.
If you use Moxy without Dagger, then presenter lifecycle is pretty simple: presenter will be alive until fragment exists and it will survive fragment's recreation. When u'll pop fragment from the back stack - presenter will be destroyed.

If you use Dagger for providing presenter then you have to manage presenter lifecycle manually, cuz you create presenter by your own. In your example you create new presenter each time when fragment calls providePresenter(). That's why you have couple of presenter's instance. You can configure Dagger to provide singleton instance for your presenter. And, sure, you will be responsible to destroy presenter when you will no need it - be carefull cuz Dagger could hold the reference to your presenter. Subcomponents could help there...
All this headacke gone if you provide presenters by default library's way, without Dagger ;)

Cheers

But my presenter is annotated with custom annotation and is a singleton. And new instance of presenter is not created each time fragment calls providePresenter(). I think if I use @ProvidePresenter then presenter is attached to Moxy life circle and Moxy is responsible to destroy them, not it?