3

In my current android projects I'm using a sqlite db to store my collections of structured data. The database is accessed by a ContentProvider called by a Loader which updates the UI.

It looks like my app is now an old-school piece of code, since RxJava became a frequently used standard, the popularity of MVP pattern has increased as it provides more reliability to applications.

After some reads I understood that Model is where data and business logic are stored, Presenter fetches data and gives it to the View, and finally the View is almost empty, it only calls the Presenter to require data and displays it.

One of the features of the Presenter should be that it exists even after the View stops existing, this meaning that when an Activity is recreated (e.g. screen rotation) the data is still there and doesn't need to be loaded again, but I see that in all the guides presenter is created in onCreate method of Activity, this means a new presenter is always created.

Am I missing something?

2

As far as I understand, storing Presenter states over Activity live cycles at the bottom happens by the old means. They are at least:

All of them need some parceling or serialization mechanism.

Here is an enlightening article about this subject:

http://engineering.remind.com/android-code-that-scales/

In the next-to-last part, the author shows a life cycle containing an orientation change:

Activity is initially created (let’s call this instance one)

  • New Presenter is created
  • Presenter is bound to the Activity

User clicks on the download button

  • Long running operation starts in the Presenter

Orientation changes

  • Presenter is unbound from the first instance of the Activity
  • First instance of the Activity has no reference, is available for garbage collection
  • Presenter is retained, long running operation continues
  • Second instance of the Activity is created
  • Second instance of the Activity is bound to the same Presenter Download finishes
  • Presenter updates its view (the second instance of the Activity) accordingly

The author has written a code example containing a PresenterManager. This is here:

https://github.com/remind101/android-arch-sample/blob/master/app/src/main/java/com/remind101/archexample/PresenterManager.java

Let's see the essential part:

...
import com.google.common.cache.Cache;
...
private final Cache<Long, BasePresenter<?, ?>> presenters;
...
public void savePresenter(BasePresenter<?, ?> presenter, Bundle outState) {
    long presenterId = currentId.incrementAndGet();
    presenters.put(presenterId, presenter);
    outState.putLong(SIS_KEY_PRESENTER_ID, presenterId);
}

To me, it remains our task to have the Presenter persistent. In the create event, as you mention, we just get their instances and use them.

  • By the way, I'd like to mention that MVP is one of many paradigms. Here is a discussion about MVP and MVVM: tech.vg.no/2015/07/17/… Often, the way people use MVP, it is similar to MVC. But, I can clearly distinguish MVVM as part of a bigger MVC structure. – peter_the_oak Feb 27 '16 at 15:55
  • Thank you for the interesting article and the code sample which perfectly answered my questions, and also for the MVVM hint! – Vektor88 Feb 27 '16 at 17:48
  • @Vektor88, I'm glad to help. – peter_the_oak Feb 28 '16 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.