35

How to separate View and Presenter in Android, while the reactions on the user actions (Presenter part of MVP) are set into the same activities that shows GUI elements (View part of MVP).

"In model view presenter just as Martin Fowler or Michael Feathers [2] say, the logic of the UI is separated into a class called presenter, that handles all the input from the user and that tells the "dumb" view what and when to display" (cited from here).

Till now I thought that one of the main features of Android is the smart Activity that takes actions, reacts to them and shows the results. Is MVP scheme in contradiction with Android philosophy? Has it sense to try to realize it on Android? If yes, how could it be done?

11
  • 2
    +1 Good question because I haven#t seen mvp/mvvm in android app sources yet. It would be interesting to see mvp android examples and how much code/lib overhead they produce. This issue was not discussed at Stackoverflow recommended-ways-to-produce-app-portable-between-android-and-other-platforms
    – k3b
    Feb 3, 2012 at 10:43
  • Maybe, I could put it into Stackoverflow, or it would be against rules?
    – Gangnus
    Feb 3, 2012 at 10:51
  • You can ask one of the admins to migrate this question insteadof duplicating the Qestion. Here on programmers the questions are more controversial question "what do you think of ../is it good or bad to ..." while stackoverflow would be more like "Are there examples of mvp in android". For me both places are ok.
    – k3b
    Feb 3, 2012 at 10:58
  • I am so sorry for my haplessness, but till now I haven't found a way to connect to admins :-(
    – Gangnus
    Feb 3, 2012 at 11:05
  • This question is NOT suitable for Stack Overflow. BTW - you can contact the moderators by flagging your post using the "flag" link.
    – ChrisF
    Feb 3, 2012 at 11:49

3 Answers 3

15

Android applications are fundamentally built around Model-View-Controller (MVC) - MVP sounds like the same thing, although I've not heard the term before. Activities fill the role of Controller, XML Views are just that (although you can build them programmatically in the Activity - it's just easier and simpler to do it in XML), and the Model you write yourself. So yes, that model is quite practical.

A possible reason you may not have heard much about this design model is that the Android framework forces you to separate the view out. Because the application on mobile devices tend to be small, people don't tend to use full-on MVC; they tend toward view and action layers where the action layer does much of the model's (small) job.

If you are writing a cross platform app, you may want to look at a four-layer approach: View, Action, Business Logic, and Model. The View and Action layers would be platform specific, while the Business Logic and Model would not change. Basically, you split out the presenter and user interaction out to the Action layer, which calls the Business Logic layer to perform the action the user wants.

8
  • +1! Please, could you give a reference or two to some good texts on it?
    – Gangnus
    Feb 3, 2012 at 14:01
  • 4
    I thought MVP offers you an opportunity to keep the Action (=Presentation) layer platform independent, too - at least, when your different platforms offer similar UI capabilities.
    – Doc Brown
    Feb 3, 2012 at 14:02
  • @DocBrown In theory, yes. In practice I'm not sure it will ever be possible, because user interactions could hit either the view or the presenter. For instance, in Android swipes are handled by the activity, but on web pages would be handled by the view (browser).
    – Michael K
    Feb 3, 2012 at 15:13
  • 12
    "Android applications are fundamentally built around Model-View-Controller" - that's just wrong on so many levels, I'm sorry. Android framework architecture is built around God Classes where View/Controller logic is cluttered. May 30, 2015 at 16:19
  • 2
    I wish I could upvote @IgorFilippov's comment more than just once. The idea that Android apps implement MVC by design is a common misconception. Contrary to iOS no GUI architecture is enforced by Android with clear separation of concerns and allowing for an isolated, easily testable Model. You have to provide one yourself, be it MVP, MVC or something else.
    – Piovezan
    Aug 26, 2015 at 0:55
6

I don't have any experience on Android programming, but having a short look into some introductory Android programming tutorials I don't see a reason why MVP should be less useful as in any other event driven framework. The Activity class is not very different from the Dialogor Form in other frameworks, so it should be easy to create an "Activitity Presenter" class for any Activity subclass of your application and put the core logic there.

Events send to your "Activity" must be delegated to your presenter, and if your presenter is going to send events on its own, or call other system dependent features, your Activity must provide related functions via the interface it shares with the presenter. But that is basically the same as in any other GUI framework I know of.

2
  • +1 thank you for you time. But as you see from another answer, in Android is used similar, but different model. "I spoke by prose for 50 years and didn't know about it!" Now I'll try to look at the differences of the models.
    – Gangnus
    Feb 3, 2012 at 14:15
  • 4
    @Gangnus: MVP is a special form of MVC, here codebetter.com/jeremymiller/2007/07/26/… you find more information. And I am pretty sure this it not a question of "either MVC or MVP". Activities may be a form of "controller", but they are platform dependent. Separating the UI logic to a platform independent presenter class for each activity will probably make them easier unit-testable and more portable.
    – Doc Brown
    Feb 3, 2012 at 14:48
5

MVP is definitely useful for Android. It helps to organise and unit test your code. And the best part is new people reading your code will be able to understand the code and will start contributing soon as they know what should go where. Here is a very helpful link to understand MVP with examples.

Here is a brief explanation of all the three components of MVP

View

In android MVP, a view contains two things Activity – android resource View – java interface Activity Implements the View and it injects itself(View interface) in the presenter so that presenter can talk to activity using view interface. First three blocks of the diagram shows the communication between View and The Presenter.

Presenter

Presenter acts as a middle layer between View and Data/Model. View(Activity) commands presenter to present something and presenter then takes data from the database/Model and gives back the presentable form of data to the View. View then takes care of displaying that data on the screen. And remember that Presenter is a plain java class it should not include any of the android components otherwise it will make the unit testing of the presenter hard.

If you wish to use database in the presenter then make activity create a database instance and inject it in the presenter. This will help you to mock the database while unit testing and will enable you to test the business logic.

Model

Model in MVP is nothing but your data source. View does not talk to data directly instead it commands Presenter to handle data for it and give the information back which can be displayed without any further modification.

3
  • 1
    Info is useful, thank you, but it is given in a bad way. You should not only cite a site, even of your own, but give info here. Reference answers are not allowed. Use comments instead. I am not flagging it only because of the useful info on your site. Please, change it into a comment or put into the answer some key info that is answering to the body of the question. Then surely you'll get not only my plus, but even the answer check.
    – Gangnus
    Sep 22, 2015 at 18:50
  • @Gangnus I have provided the necessary information in the answer because putting all the content will make the answer very big.
    – Ajit Singh
    Sep 23, 2015 at 2:49
  • 1
    There are actually two kinds of MVP. Passive View (what you described here) and Supervising Presenter, which allows direct databinding of the view to the model (much like MVVM and many web MVC frameworks).
    – RubberDuck
    Jul 2, 2016 at 17:24

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