A Simple Android (reactive) MVP tutorial

Tompee Balauag
Familiar Android
Published in
3 min readApr 9, 2018
“A series of cascades of a waterfall pouring down into a rocky pond” by Jeffrey Workman on Unsplash

Note: This tutorial requires a basic knowledge of Rx Java 2.

One the hottest topics in Android as of late is the Model-View-Presenter pattern. MVP is a UI architecture designed to separate areas of concern and promotes testability.

MVP, like MVC and MVVM, separates the view logic from the view and this new layer called presentation layer is responsible for communication between view and model. In android terms, the view will be your layout and activity, your presenter is a new class, and your model will be your business logic.

There are many variants of MVP and this tutorial will focus on Reactive Presenter. In reactive presenter, the presenter subscribe to view events and react on it. This will allow us to abstract the presenter implementation from the view, and allow conclusive unit testing of presenter without specifically testing for the view. We will also decouple the view from the presenter by introducing a contract in between. Lets get to the code.

First, we need to define base classes. This will remove a lot of boilerplate code and allow us to introduce the conventions of our architecture.

The base view is a simple interface. This will allow us to restrict the view type in presenter declaration.

Our base presenter requires an instance of the BaseView. The view is method injected. This is because in android, activities are instantiated and managed by the OS so constructor injection is difficult, if not impossible. The composite disposable is just a collection of Rx subscription for easier disposing.

The base activity at this point is actually optional. Creating one now with our current goal and assumption will not provide any benefits so I will skip that.

Exercise: Create a layout with a single button and text view and name it activity_main.

Now let us use our base class and create an example application. First, extend the view interface. This will expose all view methods to the presenter.

Now let us create an activity and load the layout from the exercise. Note that our presenter is a property of our activity. You can instantiate it on onCreate or you can use a DI container like dagger 2 and have it field-injected. For simplicity, we will be using RxBindings.

It is important that you attach the view at the earliest possible time to allow the presenter to set itself up because it is reactive. Don’t forget to detach the view as well on destroy. This will trigger necessary cleanups in presenter.

Now let us create a simple presenter that subscribe to the button click event and sets a message after.

The presenter subscribes to the button click event after the view is attached and the subscription is registered in the composite disposable. Upon view’s onDestroy, all the registered subscription will be automatically disposed so no need for manual unsubscribes.

That’s it. If you have any questions, just comment and I will try to answer them. I have a github project that utilizes this approach so if you need further reference, you can check it out.

--

--