Exploring MVP on Android

İsmail Emre Bayırlı
5 min readMay 24, 2020

--

For junior Android developers there are lots way to start developing a mobile app. When you research on internet you can find useful articles to do that. However sometimes you may not be able to understand how to use them into your project. In this article i want to explain how to use MVP design pattern on your project.

What is MVP Design Pattern?

MVP(Model-View-Presenter) design pattern is the one of the Android Design Patterns like MVC or MVVM. You may curious what are the differences between these design patterns but we will talk about these differences in another article.

In this progress i got lot of help from ekremh . Thank you brother.

Let’s start with explain what MVP is. MVP has theree main compenent. These components are Model, View ,Presenter. Let’s look at the these components each one individually.

Model: The model is the section where the data to be displayed in the user interface or processes such as sending events etc. are processed. Model provides the information that Presenter needs.

View: View is the section of where data to be displayed or interacted to user. Activity, fragment etc.

Presenter: Presenter is the section where provides communication between View and Model. Presenter gets data from Model and put them on the View. Also Presenter reacts the user interaction that comes from View.

Also each Presenter must have a View. Presenter knows its View via on interface. View and Presenter communicate each other via on interface.

Why are we using MVP?

We use MVP to write more compact, solid and understandable codes. When working with the group, it is important to use the design pattern so that everyone understands the code. And by using MVP, we have developed a code that can be tested more easily. Let’s start then.

I will use Kotlin for this configuration.

First let’s create a folder name Base. We will create three abstract class and one interface in Base folder.

Folder Structure

BaseView

BaseView is an interface that we use it for our global functions like toast message, progress dialog, etc.

As i said, i want to keep it simple as possible. Therefore, i just created a basic toast message function. We can create some other functions like progress dialog show or dismiss but this is up to you and your project’s requirements.

BasePresenter

After created BaseView we will create BasePresenter. As I said, Presenter is the application section where we do logic operations. But we are creating skaleton, therefore, we won’t do some spesific logic business like web services process right now.

As i told, every Activity or Fragment has a own Presenter and in MVP, every created presenter extends BasePresenter and every Presenter has a View. So, each view might comes from different activity or fragment. We use T and * Kotlin generic types for that. We didn’t give any type to view and we didn’t need to do that thanks to generic types.

You can look at Kotlin generics on here; Kotlin Generics

After, we created a value called isViewAlive because we want to keep the view running situation or view shutting down situation.

We define that value Atomic because when a variable is Atomic each process(thread) that want to reach that variable must be synchronized. Thus, while one View is running, we prevent another View from changing this value.

The start() and the finalizeView() functions are about that atomic boolean and you can figure out what are these functions doing from names.

Then we created overridable function called initialize. Our purpose of in this function initialize View, for Presenter. Incoming extras value could be come from Activity or could be come from Fragment. Later we will see this function’s usage.

BaseActivity

Now it is time to create Base Activity. By default, every activity on Android app extends AppCompatActivity class for Android lifecycles events. But on MVP, we are extending AppCompatActivity just in BaseActivity class. Other activities just extending BaseActivity. Let’s see the code.

As i told, every Activity has own Presenter. Therefore, we define presenter on constructor. And we used Kotlin generics again on constructor to avoid give specific type to Presenter.

BaseActivity extends one class and implements one interface. We extended AppCompatActivity class for use activity lifecycle and we implemented BaseView to use global functions that we created before.

We created two variables. One for Activity ’s layout, other one is for Activity ’s presenter. And we set these variables abstract because we must initialize layout and presenter when we create new activity.

As you know, when a class extends an abstract class, you must override abstract variables or functions on extended class.

After, we defined an abstract function called initializeUI. We used that function in a lifecycle event onCreate. onCreate function is a lifecycle event in AppCompatActivity class that works first function when an activity started.

First we give a layout for our activity in onCreate function.

And we give to Presenter intent.extras which means this an activity.

And we called a function we created before.

I used initializeUI() function in my app to set click listener for my UI components or used some first initialization variables etc. You can change it or you can do whatever you want.

After that, we used again lifecycles event to manage view ‘s situation. And we override toast function from BaseView to use it.

For at the beginning that is enough for the BaseActivity. Now i will show one more thing for if we want to use fragments on our app.

BaseFragment

BaseFragment declaration is almost same as BaseActivity. I just show you difference between them. Let’s see the code.

And you can see the codes above, we gave presenter to our fragment in constructor again and we used generic type to avoid give specific type to presenter. Instead of extend AppCompatActivity class we extend Fragment class to use Fragment lifecycle. They might has almost same lifecycles events but there is a few difference between them.

We used same abstract declaration to initialize layout, presenter and UI.

For initialize presenter and layout, we used Fragment lifecycle event called onCreateView. We gave arguments to presenter that means this is a fragment. And we use LayoutInflater to the initialize fragment ‘s layout.

onViewCreated event as you can understand from name, works after view created. We called our function in that event.

As a difference you can see the BaseActivity definition here. Fragments are not an activities. They work on an activity and they can’t work without attach any activity. Therefore, we define a BaseActivity variable called baseActivity and we set that variable as nullable.

In Fragment lifecycle events, we give a context to our nullable baseActivity. When we are done on fragment, we detach baseActivity variable. Other fragments that work on the same activity will not work if we don’t detach the variable. Other parts are same like BaseActivity.

And all configuration is done. Now you can use MVP pattern in your project. Thank you for reading this.

Stay safe.

--

--