Hiii Everyone in this article I am sharing A Model-View-Controller Framework for Android. MVC Model View Controller is a concept for design in android. In Android View like is a layout, Controller Like Ia Activity, Model like is Class whare your implement logic
Android MVC: Creating a Model-View-Controller Framework for Android
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 classes like ListActivity, TabActivity and make use of the XML file by inflaters.
- In Android, you can create much Activity for your logic.
- A lot of Utils have been already written for you – DatabaseUtils, Html.
There is no universally unique MVC pattern. MVC is a concept of a design pattern framework. You can implement your own MVC on any platform.
- Model — the data layer, responsible for managing the business logic and handling network or database API.
- View — the UI layer — a visualization of the data from the Model.
- Controller — the logic layer, gets notified of the user’s behavior and updates the Model as needed.
MVC is already implemented in Android as:
- View = layout, resources and built-in classes like
- Controller = Activity
- Model = the classes that implement the application logic
(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).
How Should MVC Be Applied in Android
Nowadays, the question of how to apply MVC patterns has an answer that is easier to find. The Activities, Fragments, and Views should be the Views in the MVC world. The Controllers should be separate classes that don’t extend or use any Android class, and same for the Models.
One problem arises when connecting the Controller to the View since the Controller needs to tell the View to update. In the passive Model MVC architecture, the Controller needs to hold a reference to the View. The easiest way of doing this, while focusing on testing, is to have a BaseView interface, that the Activity/Fragment/View would extend. So, the Controller would have a reference to the BaseView.