Writing Better Android Apps with MVP

Anup Cowkur

Why use MVP?

Get a clear picture

Most apps just use View-Model architecture so everything is connected to everything.

source: http://konmik.github.io/introduction-to-model-view-presenter-on-android.html

Using MVP gives us:

source: http://konmik.github.io/introduction-to-model-view-presenter-on-android.html

Separation of Concerns

Helps keep code decoupled

Flexibility

Each layer handles it's own business and hence is easier to modify

Easy background tasks

No lifecycle, no worries!

Testability

Test layers in isolation

Sounds good, but what does MVP look like?

How does this translate to Android?

Activities, Fragments and Widgets are all 'Views'

Data layer consists of Model objects, SQLite, API data, ContentProvider etc

Presenters get data from Data layer and update Views. They also respond to view events

Keep presenters Android free

Let Views handle everything that touches the UI thread

KISS

(Keep It Simple Stupid)

Dependency Injection really makes things much easier. 

Dagger from Square/Google is a great DI framework for Android

Mocking frameworks make testing layers in isolation easy

Mockito with PowerMock extensions is a powerful Android compatible mocking library

Let's see some code!

MVPSample project:

https://github.com/anupcowkur/MVPSample

A small sample app that uses:

  • Dagger
  • Espresso
  • Mockito and PowerMock
  • OkHTTP and Retrofit 
  • RxJava 
  • And others

Thank you!

@anupcowkur

github.com/anupcowkur

Made with Slides.com