Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
+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 '12 at 10:43
    
Maybe, I could put it into Stackoverflow, or it would be against rules? –  Gangnus Feb 3 '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 3 '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 3 '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 3 '12 at 11:49

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
+1! Please, could you give a reference or two to some good texts on it? –  Gangnus Feb 3 '12 at 14:01
3  
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 '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 3 '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 3 '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 3 '12 at 20:10

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.

share|improve this answer
    
+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 '12 at 14:15
2  
@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 '12 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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