Great Android App Architecture [Part 1 – Introduction]

Google has introduced new exciting Android App Architecture Components in this years I/0. I have been playing with those since the day they were announced and excited to share with you my experience with them! Great Android App is every Android Developers dream!

Just for the sake of learning how to use those components, I have developed sample application which you can see here at Git Hub. Sample app will simple use free REST API to fetch quotes and display them on screen. So you may ask, what’s to learn in that, I can do that without any architectural components on my own, easy peasy!

Just don’t hang up on me yet, I will demonstrate you how to create Robust, Maintainable and Testable application from this simple example. Even though using those components for such small application may seem overkill, but believe me, as your code will grow bigger (and it will!), you will thank yourself for using these components.

Application will look like something as simple as below picture (left side screenshot) at the start and we will try to improve it over time to make it full-fledged feature rich Quotes Application, keeping app maintainable, testable and robust all along looking something like the screenshot on the right side.

Our final product is going to look something like this.

Android App Architecture we are going to follow for this app will be as shown below. (Image source-Google)

This Android App Architecture is going to take care of all the common problems you might have faced while developing Android apps OR you will face when you will start Android Development. You can read more about those here.

Before diving into the App Development, let’s understand little bit about Components which we are going to use.

  • Lifecycle :
    It is an abstract class which has Lifecycle attached to it. It holds the state of lifecycle and thus objects can observe it without any extra piece of code in Activity or Fragment (less code in view part). Lifecycle aware objects prevents extra pieces of code in onDestory, onStop and onPause methods and also the memory leaks possibility which comes along with them.

  • ViewModel :
    ViewModel class is used to manage data for UI (activities and fragments) keeping loose coupling between UI and Data. ViewModel’s only responsibility is to manage the data for the UI. It should never access your view hierarchy or hold a reference back to the Activity or the Fragment. As activities are created and destroyed by android framework depending on memory constraints, it is not good practice to keep data in Activity or Fragment object as all the data will have to be generated again after Activity recreation making app inefficient and potentially more memory and battery consuming. In other words, this means that a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel. Know more about view model class here.
  • LiveData:
    LiveData is data holder and wrapper that can be observed within given lifecycle. The data wrapped in LiveData can be observed and for any change in data, observer will be notified only if LifecycleOwner is in active state. While creating LiveData observer in your activity or fragment, you don’t have to worry about removing that observer in your activity or fragment code. Lifecycle will take care of it and observers will be removed automatically once Activity gets destroyed. Read more here.
  • Room Persistence Library:
    Room is a persistent data library for implementing data persistence in your project. Room helps you store java objects in SQLite at very abstract level and thus removing db specific code in your project. At compile time, it validates each query against the schema, so that broken SQL queries result in compile time errors instead of runtime failures. Room abstracts away some of the underlying implementation details of working with raw SQL tables and queries. It also allows observing changes to the database data (including collections and join queries), exposing such changes via LiveData objects. In addition, it explicitly defines thread constraints that address common issues such as accessing storage on the main thread. More info here.
  • Retrofit :Officially retrofit is described as A type-safe REST client for Android and Java.
    Retrofit is library for making your life easy when you have to do http calls in your app. You’ll use annotations to describe HTTP requests, URL parameter replacement and query parameter support is integrated by default. Additionally, it provides functionality for custom headers, multipart request body, file uploads and downloads, mocking responses and much more. You can read more about it here and here.   So lets get our hands dirty in code next tutorial here.

I will be writing more articles about tips and tricks for making most of your android application in future, subscribe  The Tech Guru for getting the articles directly in your inbox. Subscription box can be found on top right side of this article of you are on PC and at the bottom of this article if you are on mobile device

Leave a Reply

Your email address will not be published. Required fields are marked *