Model View Presenter (MVP) in Android, Part 2

You may also like...

18 Responses

  1. Wasim says:

    Hi Tin Megali,
    Thanks for both articles and diagrams. it helps me understand how it works internally.
    I have one question
    MVP pattern works with BaseActivity Pattern ? i hope you know it but still Base activity pattern means we create all basic setup in base activity like toolbar and drawer and we extent it in other activity so we can have access to those controls every where.

    But from my understanding all activity or fragment extends presenter so is base activity pattern work with it or not. i have done some r&d on MVP but i could’t find any solution with base activity.
    Please can you help me ?

    • tinmegali@gmail.com says:

      Hello Wasim, sorry for the delay but your message was in the spam box.
      I really don’t know exactly what you mean by BaseActivity pattern. Could you elabore more?
      If I understood correctly, you just want to use an Class as a foundation for you other Activities, it wouldn’t be a problem, as long as you create the communication with Presenter correctly.
      In the article’s part 3 I talk about simple-mvp a simple laibrary that uses a GenericMVPActivity as foundation for every Activitys. So I suppose that you could extend GenericMVPActivity in you BaseActivity and use it as your foundation stone for your other Activities.

      But from my understanding all activity or fragment extends presenter

      This is wrong. The Activity and Fragment represent the VIEW layer, therefore they only communicate with the PRESENTER using an interface. The Presenter works as a mediator between View (Activity or Fragment) and the Model (data).

      Tks for you contact.

  2. Alexey says:

    Hello. I have a question about a StateMaintainer class. Why not to use Androi Activity State istead of it (http://developer.android.com/training/basics/activity-lifecycle/recreating.html)? I mean we can just store our a presenter in a bundle.

    • tinmegali@gmail.com says:

      Hello Alexey. We can’t save the Presenter in a Bundle. It would be necessary to implement the Parcelable interface in the Presenter to do so, and it would be very complicated to do it and maybe even impossible. The StateMaintainer uses a Fragment that maintain its instance, this is the official recommended approach when we need to save complex objects.
      Best,
      Tin Megali

  3. VIjay says:

    Interested in reading

  4. Max Melnychuk says:

    Thanks for the article! It’s very helpful!

  5. Shadrack says:

    Thanks for the article, now I really understand the MVP pattern.

  6. Mez says:

    Nice articles! I learned a lot from these. One question regarding the method StateMaintainer#firstTimeIn() when we catch the exception why do we return false? Doesn’t that imply we are safe to go ahead and try and retrieve some state from the retained fragment? Should we return true instead and create a new instance of the fragment as if it were the first time?

  7. thong says:

    I think this example is not good, because the view’s task is too simple (just show msg) what happen if view must show complex data? are you sure view will not call model directly ?

    • tinmegali@gmail.com says:

      Hey Thong, Sorry for the late response. I’ve been so busy lately that I hadn’t the time.
      You’re right about the simplicity of the example. I did it on purpose since the architecture part is complex enough. However, you can implement this logic on any situation, as long as the MVP principle is preserved. I did on production situations and all went well. I would advise adding some Dependency Injection to the creation of the Presenter and Model to make it even more independent for the view.

  8. Jisoo says:

    Hi!Thanks for you,
    I have one question.
    I know that MVP has a separate view and model.
    I wonder what separation means.
    Simply do not want to touch the model data in the view,
    I wonder if the view does not even know the existence of the model.
    For example, in the above example, removeNote (Note) is called by mainActivity (not actually used in source code).
    To use it in MainActivity, MainActivity needs to know the Note.
    Could you give me the information?

  9. Duong says:

    Hello tinmegali@gmail.com . I would like to ask about two point :
    1/WeakReference is used in StateMaintainer and MainPresenter, what purpose is ? Have any problem if we do not use with WeakReference .
    2/ StateMaintainer combine with an Fragment is only for saving MainPresenter in MainActivity , is not it ?
    Thank you &BR!

    • tinmegali@gmail.com says:

      Hello Duong, sorry for the late response. I’m being so busy lately that I couldn’t pay much attention to my blog.
      About the WekRederence:
      “The “WeakRederence” class lets the garbage collector finalize the value more easily, avoiding memory leaks. Once the object is not reachable (like a View after a configuration changing) the garbage collector will clean up the WeakReference. Take a look at the documentation”

      Concerning the “StateMaintainer”. It is a utility Class, so it can be used for another porpuses. I chose to write it on a separate class only to create more modularity on the project, so it could be done differently.

  10. Ahmed al-sabsab says:

    (WeakReference) I have searched a lot about it and why it’s used for performance and caching issues, can you please explain why exactly do you use it with the view objects?
    And why you use the interfaces when you create them.

    • tinmegali@gmail.com says:

      The “WeakRederence” class lets the garbage collector finalize the value more easily, avoiding memory leaks. Once the object is not reachable (like a View after a configuration changing) the garbage collector will clean up the WeakReference. Take a look at the documentation.
      Cheers!

  1. 12 de March de 2016

    […] Model View Presenter (MVP) in Android, Part 2 […]

  2. 12 de March de 2016

    […] Model View Presenter (MVP) in Android, Part 2 […]

  3. 11 de October de 2016

    […] through using interfaces and calling the interfaces instead of classes directly. (see the link: http://www.tinmegali.com/en/model-view-presenter-mvp-in-android-part-2/). In the link above the author specifies he doesn’t use callback methods but interfaces to […]

Leave a Reply to Shadrack Cancel reply

Your email address will not be published. Required fields are marked *