MindOrks
Published in

MindOrks

Android MVP with new Architecture Components

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

Checkout my latest app: Tinido.

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.

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

--

--

--

Our community publishes stories worth reading on Android Development

Recommended from Medium

Who are You Hiring and Why?

Building Data Vault modeling capability through the Mob

Blog entry: Noob robotics — The body and new to 3D printing

What is the difference between a hard link and a symbolic link?

Workshop on our Open Source Wireless Environmental Sensor

To automate Docker,Aws,Hadoop,LVM partition and some basic RHEL8 commands using Python

Post Event: Firebase Study Jam

Active Directory- Basic Concepts

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
Quang Nguyen

Quang Nguyen

Software engineer and designer, specializing in native mobile apps, currently building digital scent technology at aromajoin.com.

More from Medium

Google I/O 22: Android Keynote

Choose Kotlin for Android Application Development: Solution to Complex Enterprise Challenges

One Framework by Week

How to Intercepter HTTPS on Android With Requestly SDK