sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

How to not retain presenter #245

Closed dawidhyzy closed 7 years ago

dawidhyzy commented 7 years ago

How to not retain presenter on configuration change in Mosby 3?

sockeqwe commented 7 years ago

The corresponding Activity|Fragment|ViewGroup Delegate takes such a boolean flag as constructor parameter.

See here: https://github.com/sockeqwe/mosby/blob/master/mvp/src/main/java/com/hannesdorfmann/mosby3/mvp/MvpActivity.java#L113

So you have to override this method getMvpDelegate() (always return same delegate instance, don't create a new on each getMvpDelegate() call)

jhowens89 commented 7 years ago

I ended up doing this override, but the reasoning wasn't configuration changes.

My scenario is I have a container-like activity with a fragment that takes up this whole activity. Inside this fragment I have unrelated views and a FragmentStatePagerAdapter. This FragmentStatePagerAdapter obviously contains fragments and inside these view pager fragments I have a MviLinearLayout. When switching the adapter around, the fragments get destroyed, the views inside the fragment get destroyed, but the presenter is retained because its default behavior is to rely on the activity's state.

Any suggestions on the path forward? The way I see it, I can just say I don't care about retaining the presenter (not the end of the world) or I could have my view group delegate get a reference to my fragment and call FragmentMviDelegateImpl.retainPresenterInstance. I know you mentioned using fragment adapters a lot in your production app, so in your opinion is this a route worth going down or one I should avoid if possible?

sockeqwe commented 7 years ago

@jhowens89 could you please open a dedicated issue (just copy & paste your comment above) for that because i haven't found any general working solution for your issue since it is not easy to find out the "parent" Fragment of MviLinearLayout. maybe some other people here know a way to determine the "parent" fragment so that we can make the MviViewGroupDelegate lifecycle aware of the "parent" fragment.