MVP Implementation in Android

Shivam Sharma
3 min readJul 4, 2018

--

Credit: https://www.techyourchance.com/

In these days we add new Features or functionality in our Android Application. Due to that our source code is large and lots of files are there. To handle them and maintain the code is difficult using old approach add all logic and design in one file. So there is some Design Pattern are there to solve the maintaining the code. MVC (Model View Controller)used in Web development and MVP (Model View Presenter) or MVVM (Model View ViewModel) used in Android Developement.

In now days Mostly companies also follow these patterns to create any Android Project or web Development. I tried so many tutorial to learn MVP Design Patterns. But every tutorial is different for MVP Design Pattern. I tried to learn my self MVP Design Pattern.

In MVP contain three words M-V-P.
1). M means (Model) like Database, Shared Preference from where you get Data.
2). V means (View) like Activity, Fragment to show the UI to user.
3). P means (Presenter) Actually it is a Middle Man to make communication between View and Model like (Delivery Person) to deliver the data from UI to Database and vice versa.

I create one new Project in Android to implement MVP pattern. In this project I create one Activity LoginActivity with that I create two Interface and one more Class LoginPresenterImpl that two interfaces are LoginView and LoginPresenter following Image show you how I understand that pattern on my ways.

Class and Interface i created for MVP Pattern

Follow that I implement LoginView in LoginActivity Class and LoginPresenter implement in LoginPresenterImpl Class. In interface I declare Method to override in Class.

After that I Create a Object of LoginPresenter Interface in LoginActivty Class. and declare with LoginPresenterImpl class.

Following is LoginActivity Class

After that what Happen through that presenter Object you can pass the Value from Activity or Fragment to Presenter Class using Presenter object that values u used to store the data and send data to server you can do whatever you want.

Now I Show LoginView Interface:

Now Following is LoginPresenter Interface:

Now following is LoginPresenterImpl Class:

From LoginActivtiy class I declare presenter like this :-

presenetr = new LoginPresenterImpl(this);

In LoginPresenterImpl Class create Constuctor pass with LoginView parameter.

From that view i can access to UI and perform actions.

I hope Guys you understand how MVP work. But make sure you all try once it with your own please not copy paste the code if you copy paste the code you not understand how code is flow from Activity to Interface or from Interface to Presenter and then Presenter to Activity.

Try once.

Hope Guys its help You

Keep Coding & Be Patient

--

--

Shivam Sharma
Shivam Sharma

No responses yet