0

I am looking into MVP architecture Implementation in android. I found too many ways(mention end of the question) to implement it in the android studio, but Still, I am confused.

Can someone help me to find the right answer of below questions.

  • What would be directory structure of Application in MVP?
  • Activity should be a Presenter or View?

Way-1

Way-2

Ankit Saini
  • 374
  • 5
  • 15

4 Answers4

3

What would be directory structure of Application in MVP?

  • There is no predefined structure for that. What makes your code readable or what structure you are following, you can use same for MVP also.

Activity should be a Presenter or View?

  • View is the UI layer which displays the data and notifies the Presenter about user actions. So Activity will always be a view.

If you are looking for a good example of MVP implementation, there is one GitHub Repo for MVP developed by Android itself. Which you should look into.

Where

todo‑mvp

  • Demonstrates a basic Model‑View‑Presenter (MVP) architecture and provides a foundation on which the other samples are built. This sample also acts as a reference point for comparing and contrasting the other samples in this project.

todo‑mvp‑clean

  • Uses concepts from Clean Architecture.

todo‑mvp‑dagger

  • Uses Dagger 2 to add support for dependency injection.

todo‑mvp‑rxjava

  • Uses RxJava 2 to implement concurrency, and abstract the data layer.

todo‑mvvm‑databinding

  • Based on the todo-databinding sample, this version incorporates the Model‑View‑ViewModel pattern.

todo‑mvvm‑live

  • Uses ViewModels and LiveData from Architecture Components and the Data Binding library with an MVVM architecture.
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18298038) – Sree Dec 19 '17 at 10:46
  • 1
    @Sree You are correct. But sometime it depends which link I am sharing. That link is of official github repo of Android. – Pankaj Kumar Dec 19 '17 at 10:56
  • I had seen that sometimes official link also become invalid, eg: eclipse related android links are not working now – Sree Dec 20 '17 at 03:18
  • 1
    There is no any url in world which will not be invalid in future.. correct? Update required always.. I tried to modify my answer and hope you would be fine :) – Pankaj Kumar Dec 20 '17 at 07:39
1

Though there are lot of implementations for an MVP architecture, all of them share a basic idea (or they should at least), which is separating business logic from your views (activities, fragments, dialogs). Why is that? Well, for two reasons mainly:

  1. separation of concerns
  2. Testability: your business logic is able to be tested if there is no android components involved.

About your questions:

What would be directory structure of Application in MVP?

There is no rule about that except that your MVP components should be identified. Here you have an article where I started with a package structure but then I found other more convenient.

Activity should be a Presenter or View?

Your activity (or fragment or whatever components in charge of showing view components) should be the one that implements your view.

My advice is that you should check multiples examples and see their advantages and disadvantages of each one, and try to define your own architecture from those which you will feel more comfortable with.

Leandro Ocampo
  • 1,894
  • 18
  • 38
1

You can use either Activity or Fragment for View layer. This is because showing UI elements in android needs Context.

For the Presenter layer, you must make sure not to pass the Context to the Presenter via constructor or setter. If you needed Context in your Presenter for tasks other than showing the UI, such as writing to SharedPreferences, you can get it from your View (which is either Activity or Fragment). In this way, if the View gets destroyed or becomes null, there would no standalone null Context in the Presenter to cause leak issues.

If you want to know more about the MVP structure, I have written a very handy MVP library for android and explained its use in a sample app here.

Ali Nem
  • 5,252
  • 1
  • 42
  • 41
0

MVP android sample example application MVP Android Example used to explain how to use this pattern in our Android apps.

essid
  • 9
  • 1