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?

Gangnus
  • 2,805
  • 4
  • 21
  • 31
  • 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](http://stackoverflow.com/questions/4977147/recommended-ways-to-produce-app-portable-between-android-and-other-platforms) – k3b Feb 03 '12 at 10:43
  • Maybe, I could put it into Stackoverflow, or it would be against rules? – Gangnus Feb 03 '12 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 03 '12 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 03 '12 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 03 '12 at 11:49
  • @ChrisF. Thank you twice. --- I was very strictly reprimanded, that using shifted up letters is considered as shouting here. Are you shouting at me? Or you are the same lover of good old typewriter texts as me? :-) – Gangnus Feb 03 '12 at 11:53
  • I'm emphasising that I wouldn't migrate this question. – ChrisF Feb 03 '12 at 11:56
  • Oh, yes. I understood you that way. And I agree. But I would like to understand the system. I was told that people here take only italics and bold as emphasising and SHIFTED words they take as shouting. See comments under the question here http://stackoverflow.com/questions/8848121/why-it-is-impossible-to-access-resources-in-a-static-way. – Gangnus Feb 03 '12 at 12:58
  • @Gangnus It's an internet idiom, not specific to StackExchange. One or two all-caps words is generally used as emphasis (like the "NOT" above). More than a couple all-caps words is considered to be shouting. – Izkata Feb 03 '12 at 19:08
  • SO did have some similar question: http://stackoverflow.com/questions/8510683/android-application-architecture-mvvm-or-mvc – xandy Apr 27 '12 at 09:19
  • Yes, It provides decoupling problem for better testability and maintainability – Surya Prakash Kushawah Feb 26 '19 at 06:49

3 Answers3

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.

Michael K
  • 15,539
  • 9
  • 61
  • 93
  • +1! Please, could you give a reference or two to some good texts on it? – Gangnus Feb 03 '12 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 03 '12 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 03 '12 at 15:13
  • Also, the XML for views isn't required, just _very_ strongly suggested; you can build up a View entirely in the Activity. – Izkata Feb 03 '12 at 19:10
  • I know it and very actively use. But as I see here, it is against the MVC idea, that is important for the Android. Maybe, I should in such cases hide all code, that creates layouts, into the special classes, or even package? – Gangnus Feb 03 '12 at 20:10
  • Nothing says that your view has to be in another language (JSP, XMP, HTML...). MVC just wants it to be separated. I have seen proposals (can't find the link this second) to use the Activity/view xmls as the entire View layer and write the Action layer separately. Your approach really depends on your requirements and the size and complexity of the application. – Michael K Feb 03 '12 at 21:51
  • 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. – CheesyGnocchi May 30 '15 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 '15 at 00: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.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • +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 03 '12 at 14:15
  • 4
    @Gangnus: MVP is a special form of MVC, here http://codebetter.com/jeremymiller/2007/07/26/the-build-your-own-cab-series-table-of-contents/ 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 03 '12 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.

Ajit Singh
  • 159
  • 1
  • 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 '15 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 '15 at 02: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 02 '16 at 17:24