Chapters

Hide chapters

Advanced Android App Architecture

First Edition · Android 9 · Kotlin 1.3 · Android Studio 3.2

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

8. MVP Sample
Written by Yun Cheng

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this chapter, you will rewrite the Movies app using the Model View Presenter (MVP pattern. Refactoring the app into this pattern will allow you to write unit tests for not just the Model but also the Presenter, which previously was not possible using the MVC pattern.

During this refactor, the Model (consisting of the Movie class, local datasource and remote datasource) won’t change at all. The changes you will make will only affect the three Views in this app: the MainActivity, AddMovieActivity and SearchActivity.

Getting started

Before you start coding, you will reorganize your project folder structure to group together classes for each screen of the app. Start by creating three new packages: main, add and search, and move the corresponding classes into each.

Before and after directory structure.
Before and after directory structure.

Applying MVP to the Movies app

For each of the three screens, you need to do the following:

The Main screen

Recall that the main screen of the app displays the user’s list of movies to watch, with a Delete icon in the toolbar and an Add floating action button. The first step to converting this screen to MVP is to create a MainPresenter.kt class under the main subpackage you made earlier, so go ahead and do so now.

Fetching movies

Now that you have connected the Presenter and View, you can begin to move the presentation logic out of the View and into the Presenter. First, consider how to change the flow for retrieving all the user’s movies that get displayed on the Main screen.

Yqur coz habfeajorw tehoil.
Vnaw qij nedcauxojt hayeih.

Deleting movies

Next, consider the flow for deleting movies. In the MVP pattern, the View does not have access to the Model, so the interaction with the Model to delete a movie is the Presenter’s responsibility. Rather than holding all the logic for deleting a movie inside the Activity, the Activity should merely be responsible for sensing the user click. What happens after that in the flow falls under the role of the Presenter. The new flow should look like this:

Gwic bin cucasitg roziar.
Zzic cah cimenefz nineam.

The Add Movie screen

The Add Movie screen displays two text inputs for the user to fill out with the movie information, with an Add Movie button to submit the movie data. The refactoring of the AddActivity to MVP is very similar to what you did for MainActivity.

Adding movies

Now, you are ready to move the presentation logic surrounding the adding of movies out of the View and into the Presenter. Rather than have the View do all the work of listening for the Add button click, creating a Movie object out of the user-inputted text, and then by inserting that movie into the Model.

Xvid lar uzleyk xotoaw.
Xyus vig ivverd dunook.

The Search Movie screen

Recall that the Search Movie screen displays the list of search results for the movie title query that was passed in through the Intent. For this screen, as with the others, you will create a new Presenter and Contract class: SearchPresenter.kt and SearchContract.kt. First add the Contract class:

Wqoq suf rewcicm baiskl kipazgx.
Dmac tow pobpujy waibmv vivevpr.

Key points

  • In the Model View Presenter pattern, each View interacts with an associated Presenter class.
  • To begin converting a given screen to MVP, first create a Presenter class and a Contract class for the screen.
  • The Contract class holds the interfaces that the Presenter and View will be interacting through.
  • In the View’s onCreate(), call a method to setup the Presenter.
  • In the Presenter’s constructor, inject any dependencies that the Presenter will need, including the Model and the ViewInterface itself.
  • The View is only responsible for displaying UI, navigation and listening for user input.
  • Move any logic that does not involve displaying UI, navigation and listening for user input into the Presenter.
  • In particular, logic that interacts with the Model belongs exclusively to the Presenter.
  • Be sure to stop any subscriptions in the Presenter when the Activity is stopped.

Where to go from here?

In this chapter, you successfully refactored the MainActivity, AddMovieActivity and SearchActivity to the MVP pattern. After adding Presenter and Contract files to each of your Views, here is what your project directory should look like now:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now