Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What role an Android Service should play in the MVP pattern?

I am developing an Android app that does Human Activity Recognition.

It basically works like that - Service constantly reads the accelerator data and stores the recognized activity (i.e. Walking, running) in a database. The user can see all of the recognized activities in an ListView in activity (accesses the database). Every User table in the database has a pa_goal (physical activity goal) field which the Service reads from the database and does some checks.

The user, of course, can change this goal from an activity. Since I will be implementing the MVP architectural pattern.

I am unsure where to put the Service? It surely isn't View. Any advice?

like image 311
Georgi Koemdzhiev Avatar asked Jan 26 '17 08:01

Georgi Koemdzhiev


People also ask

What is MVP pattern in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What's one of the MVP benefits in Android?

The general advantages that one gets from implementing MVP (or similar architectural pattern like MVC, MVVM, MVVC, etc.) are: Clear separation of responsibilities between components. This separation allows for an easier understanding and maintenance of the code base.

What is the role of a presenter in MVP?

The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.

Why we use MVP architecture?

The main reason to use the MVP pattern is the KISS principle. Without using MVP, your Activity contains UI, UI logic, and data management. This means that there are many lines of code that are hard to maintain and cannot be reused. MVP splits complex tasks into multiple simpler tasks so they become easier to solve.


2 Answers

In a clean architecture, which is what I am assuming you are using MVP for, there is the idea of separating the framework from the business logic. This is essentially what a normal presenter allows you to do.

In this case its not a view you are dealing with but the principle is similar. You don't want all your business or application logic mixed in the Android code when you can separate them out for nicer, more single responsibility classes. So I would say that while it isn't a view you should still have a presenter type class (probably better to be called controller or manager maybe).

This class would be a POJO that controls how your service behaves which is easily testable with standard junit tests and service mocks. This class and the service could then be put into its own feature package and interact with the back end models the same way as your presenters.

So, in summary, the role is of another feature of your app that sites alongside the other features (which are usually just views in my experience).

Hope that helps

like image 122
MungoRae Avatar answered Oct 21 '22 00:10

MungoRae


This article helped me in a similar situation, although may not be exactly yours, the idea is the same:

https://android.jlelse.eu/android-bound-services-and-mvp-12ca9f70c7c7

Basically, the author works around the fact that a bound service is tightly coupled to an activity, and it adds extra lifecycle calls to it.

like image 44
Jose_GD Avatar answered Oct 21 '22 00:10

Jose_GD