Skip to content
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

Incorrect behavior with FragmentPagerAdapter and Dagger #246

Open
bitvale opened this issue Mar 2, 2019 · 2 comments
Open

Incorrect behavior with FragmentPagerAdapter and Dagger #246

bitvale opened this issue Mar 2, 2019 · 2 comments

Comments

@bitvale
Copy link

bitvale commented Mar 2, 2019

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

@Dosssik
Copy link
Contributor

Dosssik commented Mar 14, 2019

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

@bitvale
Copy link
Author

bitvale commented Mar 14, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants