MVP Architecture Pattern In Android

Posted by

Introductions

The previous tutorial We have been learning MVP in IOS and next tutorial We will continue implement MVP in Android. in order to integrate MVP architecture pattern in Android project we need to make some steps let them can work together between Presenter, View and Model.

Download Now

  1. What’s MVP.
    • As so many documents already explained about concept of MVP is different but they have the same point is presenter will interact with Activity or Fragment . in my point view. Presenter <-> View, Presenter <-> Activity/Fragment, View<->Model.
    • In this tutorial I will demonstrate how it work together. assume We have a list of staff getting from list and then display them to RecyclerView Start get started.
  2. Create project
    • First of all You need to create a new project by File->New you will get dialog box. You need to fill up some fields
  3. Setting up infrastructure project
    • From Root project choose to -> Project you will see list of directory structure.
    • Next step you are going to create structure folder(common, model, presenter and view)
  4. Creating classes
    • From view folder you need to create StaffView.java

       
    • Next step from model folder you continue creating a new Staff.java

       
    • Opening build.gradle take a look at dependencies

       
    • So that you have completed 20% MVP pattern. now you need to look at common folder that’s where you will some classes Presenter, BaseHolder, BaseAdapter and¬†DividerItemDecoration . Generate Presenter.java

       
    • Creating HolderBase.java and AdapterBase this class will combine with RecyclerView in order to show data.

       
    • Creating AdapterBase.java

       
    • Creating¬†DividerItemDecoration.java This will draw a line between item of Recyclerview

       
    • Now come back presenter folder You need to create StaffPresenter class let them can communicate with Activity or Fragment

       
    • As a snippet code on getStaffList func it will be declared list of staff and after 3 seconds will be updated to MainActivity.
  5. Creating acivity_main and staff_item into layout folder.
    • You are going to generate some widget recyclerview and progressbar

       

    •  
    • Let adapter can work with recyclerview you need to create StafftAdapter and map UIView to StaffAdapter

       
    • That’s all for mvp can work and update to recyclerview. the final step we will come back available MainActivity was created before. go there let declare adapter, view and presenter.
  6. Declaring Adapter,View and Presenter to MainActivity.
    • Mapping id from UI and declare presenter, adapter class

       
    • Initial recyclerview

       
    • Implement some func from interface

       
    • Full MainActivity code

       
  7. Running project and see a great result.

Conclusion

By the way you can expand func for your project. I am loving doing MVP architecture patter for all my project the both Android and IOS. in my point view it will help me a lot when I handle big project or complicated func. even though We need more code with MVP model. it doesn’t master. I think we need a standard pattern that’s easy to maintain or expand new func. especially next developer can be easy to take the format pattern.

Wish happy coding.

 

One comment

Leave a Reply

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