SuitUp
SuitUp

Reputation: 3182

What are the best practices to preserve Presenters in Android when implementing MVP?

How you do this? How do you preserve Presenters when configuration change?

Another question: is it possible to preserve Presenters when navigating away from Fragment/Activity?

I don't mind using framework for MVP such as Mosby or Nucleus, but I think Mosby's way isn't the best possible approach. Using Fragment.setRetainInstance(true) in fragments with UI shouldn't be used according to many posts here on SO, also, it shouldn't be used with backstack. Maybe there is a better way?

Upvotes: 1

Views: 1663

Answers (1)

sockeqwe
sockeqwe

Reputation: 15919

Nucleus internally uses a static Map. Actually Nucleus does more a mapping from a Request (rxjava) executed by a presenter to a presenter instance. That means that nucleus is not retaining the Presenter instance, but retaining the Request (RxJava) and reattach it to the new presenter instance.

Mosby 2.0 uses retaining Fragments or for activities lastNonConfiguration method to keep presenters. There is nothing wrong with retaining fragments that have UI. The only downside of retaining fragments are that you can't put them on the back stack. In Mosby 3.0 (SNAPSHOT available but Fragment on backstack not implemented yet) all presenters will be saved in a Map bound to the hosting activities lifecycle. That means, Presenter can be kept for non retrainig fragments on the backstack. In contrast to Nucleus, they are only kept as long as Activities lifecycle or removed from backstack by the user (navigation). Furthermore, this allows Mosby 3.0 to support custom views (FrameLayout etc.) too.

is it possible to preserve Presenters when navigating away from Fragment/Activity?

Yes, but why need you to? If the view is not visible at all, is there really a need to keep the presenter? Nevertheless, both Nucleus and Mosby can do that.

Upvotes: 2

Related Questions