Android MVP explanation
Asked Answered
I

7

9

I try to understand what the best way is to program a "clean" Android app. I found an interesting article about it where they use a kind of a 3 layer architecture.

Now I read that android uses the MVP design pattern and try to understand this. The way I understand the MVP principle (in the android context) is that:

  • the Model layer are the logic classes
  • the presenter are the classes that are "linked" to an XML (the activities)
  • the passive view are the XML's.

Is this correct?

When this is correct, is it then also correct that the 3 layer model fits perfectly in the MVP principle because:

  1. the presentation layer from the 3 layer architecture fits in the presenter layer of MVP
  2. The business and the data layer from the 3 layer architecture fits perfectly in the model part of the MVP?

I hope my questions are clear because it is a complicated topic for me.

Maybe this clears up my vision (way of thinking) about this... Maybe this clears up my vision about this...

Illstarred answered 28/12, 2015 at 14:32 Comment(9)
I wouldn't say MVP works perfectly for every application of android. It depends entirely on what you want to do. I have used MVP for my own android app and I think it cleans up the code heavily, from having the bulk of the code in your Activities and Fragments. That being said, there was an interesting talk from Yigit - youtube.com/watch?v=BlkJzgjzL0c which doesn't seem to use MVP. Another really interesting article can be found here about MVP: medium.com/ribot-labs/…Varini
Thanks, i will watch the video's tomorrow. But do you know if that what i wrote about MVP is correct?Illstarred
Another really good example of using MVP can be found here : codelabs.developers.google.com/codelabs/android-testing/… . Which does describe how to implement MVP. I would say the View is the Activity/Fragment, the presenter is a seperate class that performs actions on the view and the model is the POJOs related to the view.Varini
I think the business layer can be in the model or the presenter layer, depending on what seems more suitable.Consanguineous
Ok, i can find me in that, makes senseIllstarred
There is a MVP sample application that you can check here: github.com/renaro/tinder-like-app , also you can understand more about MVP in this video here: youtube.com/watch?v=iXDAcWEhYSk&t=5sSeitz
I've been thinking about this for a while and have started writing a blog on the topic if you're still interested: cj65535.blogspot.com.au/2017/03/…Licko
@CJames I dont need it anymore but i checked your blog, looks nice. For everyone else, the link is out dated, the new link is: notesofapragmaticprogrammer.wordpress.com/2017/04/06/…Illstarred
@RoDo Thanks. I've been working on a new framework trying to take the sting out of Android MVP development. It's at github.com/cjsoftware-lib/ucsFramework if you're interested. It uses annotation processors to support automatic UI/Background threading and UI state preservation.Licko
K
11

first thing I wanted to clarify is that MVP or any other pattern for that matter is no specific of Android dev and, can be applied to any other framework.

I think you got the gist of it:

  • view is usually implemented by activities and fragments and will contain the reference to the presenter
  • the presenter is that middle man between the view and model. Retrieves data from the model and returns it already formatted to the view so it doesn't need to do anything else but display it.
  • the model can be seen in a simplistic way as the "data provider". It can be as complex as you want, using internal db, lots of clases etc.

If you are interested in Android apps architecture I suggest you watch this talk given at Android dev summit this year. It is one of the bests I've seen

https://www.youtube.com/watch?v=BlkJzgjzL0c

Kahl answered 28/12, 2015 at 14:47 Comment(1)
Thanks, this explains a lot to me and i think my vision of the MVP in the Android case is right. Now i'am curious if my association with the 3 layer architecture is right too.Illstarred
H
2

Even though this question has an answer, I don't think this answer is complete by any means.

MVP is a general concept which can have many various implementations, some of which differ substantially. Moreover, the concept itself is very ambiguous - different people can have different concepts in mind when they say MVP. One of the most widespread ones is shown in the below picture:

enter image description here

Regardless of implementation, the general definitions of MVP components are:

  • Model: abstraction of "application state storage". The definition of what the "state" is and how it is stored are implementation details. Model implementations should not have dependency on View or Presenter.
  • View: abstraction of "user interface". The definition of who the "user" is and how it interacts with the View are implementation details. View implementations should not have dependency on Model or Presenter.
  • Presenter: encapsulates application's business logic. Presenter processes user input events received from view, and alters application's state stored in model in response. Presenter also processes changes of application's state stored in model and updates view in response. Presenter usually depends on both the View and the Model.

If you need more information about MVP in context of Android development, you can find it in this post: MVP and MVC Architectural Patterns in Android

Hispanic answered 11/11, 2016 at 13:39 Comment(0)
C
2

Important issues which need to be addressed while implementing MVP in android are activity leaks which cause memory leaks and app crashes due to background process updating closed activity.

Due to presenter having reference to activity, if presenter can't be garbage collected, activity will stay in memory.

Both of the issues can be solved by using life cycle methods of activity or fragment and releasing resources in those methods so that memory leaks and app crashes are prevented.

Cleaning up of resources related background work can be easily implemented using RXJava, for more information about MVP and MVP with RXJava, see http://www.zoftino.com/android-model-view-presenter-mvp-pattern-example

Chantel answered 9/11, 2018 at 4:44 Comment(0)
O
1

Here on github https://github.com/saksham24/Android-Firebase-Mvp-Mvc-Mvvm-chat i made a repo containing 3 applications with same functionality but written in 3 different android patterns(Mvc, Mvp, Mvvm)

Understanding three different pattern is quite easy if we get a simple good example on them so i made a repo to contribute my knowledge to this developer community. Also the repository is written using proper java guidelines and conventions(including naming and packages, modules) so people looking for such project can also view this repository.

now

  1. if you want to know the difference between android Mvp,Mvc, MvvM see this explanation by realm https://academy.realm.io/posts/eric-maxwell-mvc-mvp-and-mvvm-on-android/

  2. if you want to compare three pattern see this wonder full blog https://thinkmobiles.com/blog/mvp-vs-mvvm-android-patterns/

Ophelia answered 29/8, 2017 at 7:23 Comment(0)
A
1

Here is simplest way to implement MVP pattern in your application android_mvp_login_sample

Alessandraalessandria answered 16/12, 2018 at 13:27 Comment(0)
W
0

As you have come to know basics of Clean Architechure. The following example depicts how actual your MVP pattern is implemented.

Example:

interface BaseContract {
        interface BaseView {
            //Methods for View
            void onDoSomething();
        }

        interface BasePresenter {
            void doSomething();

        }
    }

    class BaseMainPresenter implements BaseContract.BasePresenter {
        BaseContract.BaseView view;

        BaseMainPresenter(BaseContract.BaseView view) {
            this.view = view;
        }

        @Override
        public void doSomething() {
            if (view != null)
                view.onDoSomething();
        }
    }

    class DemoClass implements BaseContract.BaseView {

        //Create object of Presenter 

        /****
         * Example :
         * BaseMainPresenter baseMainPresenter = new BaseMainPresenter(this);
         */
        @Override
        public void onDoSomething() {
            //Deal with Context here.
        }
    }

Refer below link for sample Actual implementation with scenario & learn more about Clean Architechure : https://github.com/android10/Android-CleanArchitecture

Winegrower answered 21/2, 2017 at 14:9 Comment(0)
A
0

Now I read that android uses the MVP design pattern and try to understand this. The way I understand the MVP principle (in the android context) is that:

  • the Model layer are the logic classes

  • the presenter are the classes that are "linked" to an XML (the activities)

  • the passive view are the XML's.

Is this correct?

Not fully: for the Model layer it is true, but for the Presenter it is not. The Presenter is not linked to XML although it has reference to the View through its constructor. The View is the Activity/Fragment in android.

You might want to check here for a sample MVP app for android.

Alamode answered 19/4, 2018 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.