简体   繁体   中英

Android MVP - should an Activity be a View or Presenter?

I want to implement my next application with MVP pattern, so I started of reading some articles about how it should be implemented. The main problem for me is that there are different approaches to this pattern. Some people say that we should treat activity as a view but some others that activity should be a presenter.

Activity as a view is described here: MVP Android and it makes sense. But on the other hand I found this answer with a few upvotes https://stackoverflow.com/a/7609943 and someone says that activity should be a presenter.

Does anyone has an experience with this pattern?

After a moment's thought I think Activity should be considered as a View. If we separate business logic from activity then it will be easy to replace activity with a fragment or a view. We even will be able to take our models and presenters and use them in desktop application, just adding new views to them. It is also better for testing purpose to create presenter as an normal object, not activity.

Activity is very close to your layout so it should be a view. And your business logic should be in the Presenter created by your Activity. To understand more about MVP take a look at - MVP for android

在此处输入图片说明

I think it is safe to consider Activity to be a Presenter. The View can be considered as the layout XML file. The presenter is something which has direct connection to Model(s) as well as View(s) as said in the answer you posted above. In an Activity, you connect to the View(s), and stays as in intermediary between the View(s) and the Model(s), which is effectively the functionality of the Presenter. It takes input events from the View(s) and set the value(s) received from the Model(s) to display in the View(s).

Take a look at G+ community Android MVP and especcially for the sample https://github.com/spengilley/ActivityFragmentMVP

It's a Passive View pattern implementation, best for use in tests.

Activities should be views, since it's where graphics are rendered. Presenters and models can be written in pure java and easily tested.

See this AndroidMvc/Mvp framework

https://github.com/kejunxia/AndroidMvc

Also check out the MVP sample here https://github.com/kejunxia/AndroidMvc/tree/master/samples/simple-mvp

The term View is overloaded here, android view is different than the view supposed to be used in the MVP pattern. View is an interface supposed to be implemented either by the Activity / Fragment. You can have a look on Official Android MVP Examples .

I will suggest to start it from the basic one . Here is a flow from the page.

在此处输入图片说明

We must follow Single Responsibility Principle , when deciding wether activity should be view or presenter component.

It is nearly impossible to separate business logic from activity. One Big Reason for this is

Activity extends Context.

Functionally, Context objects provide access to most platform's features that third party applications can use.

Since Activity is Context's subclass, our applications use its API in order to take control over a subset of features and resources of the platform. And the logic that uses these features and resources is our business logic. Therefore, no matter how hard we try, we will not be able to completely separate business logic from Activities.

Since we can't separate business logic from Activities, we shall separate all UI logic from it. This is not a trivial task, but it is very well worth the effort in a long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM