How to apply MVP pattern to android project

4k views Asked by At

Sorry about my English grammar. My question is little bit stupid but I want to understand clearly about how MVP pattern apply in real application.

I'm developing an android project. I want to apply MVP pattern to my project. I refer to this demo :

https://github.com/antoniolg/androidmvp

I'm very excited with this pattern. But in this demo project, I see each activity (a view) , we always have a presenter and an interactor (model) and something else. So in project which have many screen :

  • How should I manage the presenter and model.

  • With each activity (example LoginActivity), I create a "login" package and put all presenter and model into it. Is it ok ?

  • Can someone give me a small application source code using MVP pattern. (not a demo project).

3

There are 3 answers

2
strike On

Few advice and suggestions from my end:

1: Use proper folder structure for your files.

com.app.<appname>.activities
com.app.<appname>.fragments
com.app.<appname>.adapters
com.app.<appname>.models
com.app.<appname>.api
com.app.<appname>.listeners
.
.
.

2: Create app's Application class, Initialise global 3rd party libraries instances in it and create Application class SingtoneInstance to access them.

3: Separation of Views and Business Logic layers for following along with listeners:

APIRequest
DBRequest
ImageCaching
.
.
.

4: Some standardised and common practices : Using proven and stable libraries and Bridge them with your application Business-layer and View-layer. Few libraries like

 API Framework   : Volley
 Image Framework : UniversalImageLoader/Picaso
1
Yanislav Mihaylov On

I would say something which is not very helpful but it is up to the project and you :-). Personally I'm having on top level subpackages - presenter, view, model. And we are putting the classes into them. The reason is that in the most cases the model is one for the whole app. And also there are cases (I know they are rare) when a few presenters might share one and the same View.

Kind Regards

0
Ajit Singh On

To apply the MVP pattern in android, you need to understand what MVP exactly means when it comes to android. How Model, View and presenter communicates with each other. Here is a diagram to understand it better.

enter image description here

I have applied this pattern to one of my application and it enabled me to unit test my core logic and organise the code better.

You can refer to this link to understand the MVP and the application which I created using MVP.