Android tutorial

Android MVC: Creating a Model-View-Controller Framework for Android

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 ListActivityTabActivity 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:

  1. View = layout, resources and built-in classes like Button derived from android.view.View.
  2. Controller = Activity
  3. 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

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.