Why use MVP?
Get a clear picture
Most apps just use View-Model architecture so everything is connected to everything.
Using MVP gives us:
Separation of Concerns
Helps keep code decoupled
Each layer handles it's own business and hence is easier to modify
Easy background tasks
No lifecycle, no worries!
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
(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!
A small sample app that uses:
- Mockito and PowerMock
- OkHTTP and Retrofit
- And others
Writing better Android Apps with MVP
By Anup Cowkur