Android MVP with new Architecture Components

Image Source: https://goo.gl/OPfaAq

We had been developing Android apps for a long time without any official architecture guidelines. It created challenges for us to build robust apps. Thanks to Android active community, however, great developers out there gave us directions to follow several good architecture patterns (MVC, MVP, MVVM, and Clean Architecture).

Recently, as introduced in IO 2017, new Architecture Components is a nice move of Google Android team. Personally, I considered it as a major milestone for Android development in which all developers should care about a good and suitable architecture to develop apps which are easy to test, maintain and extend.

Although Google chooses to favor MVVM as their opinion, all of us can get benefits from Architecture Components no matter which architecture our’s app is built on. Below is pointed out in Google’s blog post.

Android Architecture Components work together to implement a sane app architecture, while they individually address developer pain points.

So, what are the benefits that new arch components (LifeCycle and Room) provides us to solve our pain points ?

  • Persist data in SQLite in a clean, robust way.
  • Manage Activity/Fragment lifecycle easily.
  • Reduce boiler plate codes.
  • Avoid common errors (because it helps us to check common errors in compilation time and force us to write codes in correct way).

MVP with Architecture Components?

Google provides two basic sample apps and a advanced MVVM-based app on their Github repository. It is a really good resource to learn about new arch components.

However, as we discussed above, any app can take advantages from LifeCycle and Room. So, I would like to bring it into existing Android MVP projects.

To demonstrate it, I have created a sample app which loads newest Android questions from StackOverflow.

As normally, this sample uses:

  • Retrofit + OkHttp to consume StackOverflow APIs.
  • UI components: RecyclerView + CardView + ConstraintLayout .
  • Dagger2 for dependency injection.
  • RxJava to handle data stream.

With new Architecture Components, two main changes are added:

  • Lifecycle-aware Presenter created with support from LifeCycle.
  • For local data persistence, it is relatively easy to handle by using Room .

For data layer, this project followed repository pattern. If your project grows as a big, you can consider to separate this layer as a independent module instead of a package to decouple it with other parts and easily to extend and maintain.

This project can be used as a boiler plate app for your next MVP apps which adapts with Android new Architecture Components.

All of codes can be found on Github repository.

What’s next?

It is just a starting point for us to see the benefits of new arch components, especially for MVP projects and how popular frameworks cooperates together.

So, I am certainly going to update it with better approaches. Besides, any PR(pull request) is highly appreciated. Let’s learn together in our long journey.


Finally, if you liked this post, please hit ❤ , so other people can read it too.
Please get in touch with me via Github, Twitter, Facebook, or LinkedIn.

Happy coding and have a good time!