MVP Pattern Android for Big Apps

I try to comprehend the MVP Pattern for Android applications. The very essence, it seems, is clear, but for very small applications, which are usually in the examples. When creating a large application, a number of questions arose, namely::

  1. For example, in the 10 activity app. Should I create 10 presenter and model? I saw the information that 1 view = 1 presenter.
  2. Should I create a presenter if it is accessed only once in a particular activity and it calls the same method in the view, where the intent is implemented to move to the next activity? Or can it be done directly from the view?
  3. How to properly implement the" unsubscribe " presentera from the view, in order to avoid memory leaks?
Author: Kromster, 2019-04-09

1 answers

1)For example, in the 10 activity app. Should I create 10 presenter and model? I saw the information that 1 view = 1 presenter.

Yes, it is better to stick to the 1 presenter - 1 view pattern. An exception if some logic is frequently reused. But if it is reused, then there is a high chance that the view is also used, which means that you can separate this logic into a separate bundle of view-presenters.

2)Should I create a presenter if in a specific activity to it they are accessed only once and it calls the same method in the view, where the intent is implemented to move to the next activity?

Here it is important to distinguish between events in the presenter:

1 type - If the event is such that the data should be saved when the view is recreated, then this should be done through the presenter, so as not to go for the data twice. For example, you clicked on a button to download a news list. It was downloaded, you cached it in the presenter(alternatively, you saved it in the global folder variable in the presenter) and displayed. The user turned the screen, the data from the cache was loaded, instead of making a new request.

2 type-Navigation events, toast display, and similar-these are events that must occur once. You don't want the toast with the error to appear again when the screen turns.

Now the answer.

If this is an event of the first type, then it is obvious that this should be done through the presenter. This is just one of the cases for which we create it for. If this is the second type of event, it is up to you or your team to decide. The main thing is that the approach should be consistent. If you have made a presentation on one screen for your case with the intent, and you have not done it on the other screen, then it is confusing when reading the code and it is better to stick to one style.

3)How to properly implement the" unsubscribe " presentera from the view, so that avoid memory leaks?

In onDestroy in Activity, call the method the presenter, in which you will see a link to the view.

In onDestroyView in Fragment, do the same if you use fragments

 7
Author: Valgaal, 2019-04-09 13:16:17