Android MVP with new Architecture Components

Quang Nguyen
Jun 13, 2017 · 3 min read
Image Source: https://goo.gl/OPfaAq

Subscribe to the latest blog posts and projects.

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.

If you are interested in my new blog posts and projects, you can subscribe to my newsletter by clicking the below link.

Subscribe to the latest blog posts and projects.

Or please get in touch with me via Github, Twitter, Facebook, or LinkedIn. Happy coding and have a good time!

MindOrks

Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store