14

I know there have been quite a few questions about this, however, I'm still struggling to understand what role the Activity class should play when implementing the traditional Model-View-Controller design pattern on Android?

My gut feel is that it should be the Controller, however that means a one-to-one relationship between UI screens (since you must have one Activity per screen) and controllers, which defeats the point of MVC's loose coupling between the different components.

donturner
  • 17,867
  • 8
  • 59
  • 81
  • [Hope you would have seen this already :)](http://stackoverflow.com/q/2925054/593709) – Adil Soomro Feb 15 '12 at 10:55
  • Yes I have :) It seems that there's only one comment on an answer which is relevant though: http://stackoverflow.com/a/2925368/824903, and that basically says "Android doesn't do MVC". I was hoping for more clarity around the issue. – donturner Feb 15 '12 at 11:08

4 Answers4

10

You are right. The xml interfaces could be defined as the View and your other class working with data as the Model.

The activity receive all the events and user inputs from the View ,so, we can easily said that it is the Controller.

But let's be clear , it's not a perfect (does it really exist ?) MVC

Have a look to this question , and more specifically , the first comment of the accepted answer, it may be useful

Community
  • 1
  • 1
grunk
  • 14,718
  • 15
  • 67
  • 108
  • 1
    So basically Android doesn't do MVC "out of the box"? – donturner Feb 15 '12 at 11:43
  • In one hand the android sdk let you separate the different layers of your app. but in the other hand you can build a view directly with code in the activity. So YES Android can let you implement MVC, but NO it's not a strict application of the pattern. I'm not a design pattern expert so maybe someone else can clarify that better than me :) – grunk Feb 15 '12 at 12:42
2

Activities can make competent controllers and, with fragments, you can also implement hierarchical MVC. Most of making MVC work is up to the programmer, because even in the strictest frameworks you can still find ways to do crazy things.

Community
  • 1
  • 1
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
0

Android does not have a good architecture and it does not follow the MVC design pattern.

The best way to conceptualize Activity in MVC is as a View (with some controller logic) because it gets destroyed every time that a configuration change happens, such as a screen rotation or locale change, losing all its state.

The controller in this case would be the Application object, because it is your bridge to access the view (Activity and its GUI components) from code outside the Activity context. You have to use singletons so that there is only one Application object at a given time, otherwise there will be one Application object per process. The Application object is not a good place to store the Model because its life cycle is detached from that of the Activities. It can get destroyed and recreated at any point of an Activity's life cycle (while the application is in the background) without causing it to restart, and then all its variables and references not assigned inside its onCreate() method will become null.

The Model therefore has to be stored on a GUI-less, headless fragment with setRetainInstance(true), that is linked to an activity, and has to be re-attached to it every time the activity is recreated. You have to use a fragment manager to ensure you are recovering the retained fragment and not creating it anew. This is also the best place to run background tasks, though you can also run them in the Application object. Never run tasks on an Activity as they will get destroyed on configuration changes.

Carlos Hoyos
  • 694
  • 9
  • 10
  • The fact that Activity is recreated by the system on screen rotation does not make Activity a "View". Activity handles navigation, interacts with the system resource, it definitely is the Controller, and the android.view.View class is the "View" by actually displaying content – ssynhtn Nov 27 '20 at 01:19
0

I think the confusion may come in with defining a view as XML and so the Activity is mistaken for being the view. It's not. You pass the view (your XML layout) into the Activity which will then inflate the views contained within the XML layout. Your activity also passes data (models) into your views (EditText, TextView, extended version of base views, etc).

If you really want MVC then you can achieve this simply by setting up your view in an XML layout, create a view object which extends from your root view (if it's a RelativeLayout extend from that). From there you add your accessor methods and different logic needed for that view and you can then add unit testing around that.

In the Activity you will no longer pass in the ID of the layout and will instead do something like this:

CustomView customView = new CustomView(...);
setContentView(customView);
...

Of course almost all apps won't do this and you really don't need to do this. Calling findViewById is enough to link to that view object and call on the methods it has. Which in my mind, is MVC.

CodyEngel
  • 1,501
  • 14
  • 22