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?
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?
Although this post seems to be old, I'd like to add the following two to inform about the recent development in this area for Android:
android-binding - Providing a framework that enabes the binding of android view widgets to data model. It helps to implement MVC or MVVM patterns in android applications.
roboguice - RoboGuice takes the guesswork out of development. Inject your View, Resource, System Service, or any other object, and let RoboGuice take care of the details.
Model View Controller (MVC)
Description:
The MVC pattern is essentially this:
Important feature of MVC: We can modify Either the Model or View or Controller still not affecting the other ones
When we apply MVC, MVVM, or Presentation Model to an Android app, what we really want is to have a clear structured project and more importantly easier for unit tests.
At the moment, without a third-party framework, you usually have lots of code (like addXXListener(), findViewById(), etc.), which does not add any business value.
What's more, you have to run Android unit tests instead of normal JUnit tests, which take ages to run and make unit tests somewhat impractical. For these reasons, some years ago we started an open source project, RoboBinding - A data-binding Presentation Model framework for the Android platform.
RoboBinding helps you write UI code that is easier to read, test and maintain. RoboBinding removes the need of unnecessary code like addXXListener or so, and shifts UI logic to Presentation Model, which is a POJO and can be tested via normal JUnit tests. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality.
After some searching, the most reasonable answer is the following:
MVC is already implemented in Android as:
Button
derived fromandroid.view.View
.(This by the way implies no application domain logic in the activity.)
The most reasonable thing for a small developer is to follow this pattern and not to try to do what Google decided not to do.
PS Note that Activity is sometimes restarted, so it's no place for model data (the easiest way to cause a restart is to omit
android:configChanges="keyboardHidden|orientation"
from the XML and turn your device).EDIT
We may be talking about MVC, but it will be so to say FMVC, Framework--Model--View--Controller. The Framework (the Android OS) imposes its idea of component life cycle and related events, and in practice the Controller (
Activity
/Service
/BroadcastReceiver
) is first of all responsible for coping with these Framework-imposed events (such as onCreate()). Should user input be processed separately? Even if it should, you cannot separate it, user input events also come from Android.Anyway, the less code that is not Android-specific you put into your
Activity
/Service
/BroadcastReceiver
, the better.The best resource I found to implement MVC on Android is this post:
I followed the same design for one of my projects, and it worked great. I am a beginner on Android, so I can't say that this is the best solution.
I made one modification: I instantiated the model and the controller for each activity in the application class so that these are not recreated when the landscape-portrait mode changes.
There is no universally unique MVC pattern. MVC is a concept rather than a solid programming framework. You can implement your own MVC on any platform. As long as you stick to the following basic idea, you are implementing MVC:
Also think about it this way: When you program your model, the model should not need to worry about the rendering (or platform specific code). The model would say to the view, I don't care if your rendering is Android or iOS or Windows Phone, this is what I need you to render. The view would only handle the platform-specific rendering code.
This is particularly useful when you use Mono to share the model in order to develop cross-platform applications.