Android Life Cycle Components

With the recent releases of Android, Google has introduced new way to build your App life cycle by using their architectural components. Here I am going to introduce you few of them. But first, you need to add few new gradle dependencies to your app in order to make use of the new life cycle components.

implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

Either you can include whole extension or you can include individual components as well.

Below are the commonly used Life Cycle methods.

  • ViewModel
  • LiveData
  • Room
  • Paging

First we look at how we can use ViewModel and LiveData in our app. Basically, when we are designing Android apps you may have to define Model classes as a bottom layer of our architecture. Those POJO models are used to define your DB tables, JSON mappings such as server requests and responses or simply it can be used as UI to controller data definitions.

Every time you update your Back-end or every time you update your Front-end, data should flow both directions seamlessly. If you are using traditional AsyncTask operations to UI and low level component communications, it is really a mess because you have to take care of a background task execution and it is harder to implement Observer Subscriber-like patterns that way. Simply, If you are expecting real time behavior from your App, LiveData with ViewModels are the best in the real-time business. There are lots of advantages other than this such as caching but I am not going to explain those concepts here.

I will explain the LiveData implementations with some code samples, then you will get some idea. I have a simple App to get device orientation details from sensor and I use LiveData to real-time update my UI with Azimuth, Roll and Poll angles.

Here is the link to GitHub Code

I have created a model class called Statistics to construct my device orientation data. Then I have created a ViewModel class which is extended the Android ViewModel. The purpose of this is to update our view and hold the data changes while the state of the phone is changed (such as screen rotations) and maintain the app consistency.

public LiveData<Statistics> getStatistics() {
if (statistics == null) {
statistics = new MutableLiveData<Statistics>();
loadData();
}
return statistics;
}
‎

‎LiveData is a lifecycle aware observable data holder that is, it supports observer-subscriber pattern and whatever the data changes occured while calling the loadData method it notifies the UI accordingly.
You can load any kind of data to MutableLiveData component, such as network responces etc. , in my case it is sensor details.

In your loadData method do not foget to call ‘statistics.postValue(statistics);’

Finally you can implement your subscriber or subscribers which means your UI component

In your Activity or Fragment,

StatisticsViewModel statisticsViewModel = ViewModelProviders.
of(getActivity()).
get(StatisticsViewModel.class);

statisticsViewModel.getStatistics().observe(this, new Observer<Statistics>() {
@Override
public void onChanged
(@Nullable Statistics statistics) {
azimuthTxt.setText(String.valueOf(statistics.getAzimuthAngle()));
rollTxt.setText(String.valueOf(statistics.getRollAngle()));
pitchTxt.setText(String.valueOf(statistics.getPitchAngle()));

timeElpsTxt.setText(String.format("%d Min, %d Sec",
TimeUnit.MILLISECONDS.toMinutes(statistics.getTimeElapsed()),
TimeUnit.MILLISECONDS.toSeconds(statistics.getTimeElapsed()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(statistics.getTimeElapsed()))
));
pointsCountTxt.setText(String.valueOf(statistics.getPointsCount()));

}
});

The onChanged method loads relevent data to your UI. Simple as that.

Happy Life Cycle….