Monday, 20 July 2015

MVC pattern in Android?

MVC pattern in Android?

Is it possible to implement the Model-View-Controller pattern in Java for Android? Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android?
Your question is very good. But the answer marked as solution is not correct in my opinion. It might misguide several people. –  Saghar Jan 16 '14 at 15:33
Check out my 2 posts starting here Android Architecture: MV? –  Dori Apr 3 at 17:32
Question is very good. –  Sreekanth Karumanaghat Jun 15 at 7:20

19 Answers

up vote150down voteaccepted
In Android you don't have MVC, but you have the following:
  • You define your user interface in various XML files by resolution, hardware, etc.
  • You define your resources in various XML files by locale, etc.
  • You extend clases like ListActivityTabActivity and make use of the XML file by inflaters.
  • You can create as many classes as you wish for your business logic.
  • A lot of Utils have been already written for you - DatabaseUtils, Html.
if you're trying to imply that a static xml file is the view and an activity is a presenter/controller then you're missing the part of MVC/MVP pattern actually decouples the view and presenter. You cannot instantiate an activity without talking to your layout/view. Really what you want to do is use composition and embed the activity/layout into a view class and the have all the application/presentation logic decoupled out into their respective classes. While what you describe is MVC... it's a very bad, strongly coupled MVC. Which is poor to work with. Especially if you wish to unit test. –  JDPeckham Apr 14 '11 at 23:20 
Android is terrible at MVC. Android's API philosophy is template/inheritance over composition; which makes it bad for testing too. That being said, there are ways to get MVC out of android, but it is not intuitive. –  Paul Nikonowicz Dec 13 '11 at 16:57
I'm writing a Android Architecture series which teaches/demonstrates how to implement MVC (and other patterns) in Android. Check it out: – user123321 Dec 20 '11 at 3:02 
This is what Google itself says about MVC in Android: No results found for +mvc –  18446744073709551615 Apr 20 '12 at 9:29 
For anyone who says that "Android is MVC" please try Backbone.js (yes, client side js) for a week, and then come back and say that "Android is MVC". You'll finally understand the question and why we keep asking :) –  Mark Peterson Feb 13 '14 at 17:12

No comments:

Post a Comment