Life saving LiveData class Android

Taman Neupane
4 min readMay 20, 2017

--

What is LiveData?

LiveData is just released holder class which holds and updates activity/fragment keeping in mind about the state of activitiy/fragment. Holding is generally done by any model or pojo class but for update it uses special function called observer which will update activity/fragment instantly if any thing changes of LiveData class (seems cool)

Situation Before

  1. Generally all the application talking to the server with the help of API sends request (GET, POST, DELETE, PATCH etc) with the help of Retrofit or Volly or any other means. Updates the view(activity/fragment) ondata received from the server by the means of any callback or any other means. So in such situation if the network is lagging it takes alot of time to fetch data and its not sure the user is still in the same activity/fragment. In such case if the callback takes place then it won’t find the view and the application crashes. But generally we manage or handle such crashes using alot of boilerplate codes.
  2. Another situation is that when we refresh our view or download additional data we need to again update the view by ourself.
  3. You have seen some cases like LocationManager and Map. We need to override onStart() and onStop() for those in the activity but the location manager may be in different class because we donot want activity/fragment to look messy . So this create unwanted link between activity/fragment and LocationManager class.

1. Now its time to forget such crashes and situation because developers working out in Google recently shipped new class to handle such situation and the class is LiveData

2. Now we donot need to update view every time our LiveData class does this for us.

3. So its time to make onStart() and onStop() empty or minimize them.

So now its time to address some advantages of LiveData class.

  1. Zero activity or fragment not found exception: As it uses observer to update data and observer is well known about the state of activity/fragment.
  2. The data you get is all updated: As LiveData class get latest updated data but couldnot find activity or fragment then they just hold those data and next time when activity or fragment is resumed the observer will fetch updated data itself and provide it to activity/fragment.
  3. Easy configuration change handling: As the observer fetch latest updated data so there wont be any cases for configuration change handling but if you want to make code more configuration change handling please have a look at new ViewModel class.
  4. Communication: For activity/fragment there inter communication can also be handled by observer and its just simple. Just create the LiveData class any where and you can observe those class from multiple fragments/activities. Thanks to ViewModel for this.
  5. No need to take tension for life cycle: our LiveData class observer will already know about the state of activity/fragment it is observing so they will handle them by default.
  6. Zero memory leak due to observer: our ovserver are bound to activity or fragment so they will be destoryed when the activity/fragment is destroyed. No need to handle it manually.

Some methods we must implement in LiveData extending class:

onActive() : This method will be called when the LiveData class has active observer which means when the activity or fragment containing observer is active. Or this can also can be treated as onStart() of activity or fragment containing its observer.

onInactive() : this method will be called when there is no any active observer of LiveData class. Just the case of onStop() of activity/fragment containing observer of LiveData.

setValue() : with the help of this method we set data to the LiveData class and all the active observer connected to this class will be notified about the data change.

LiveData Transformation

There might be three cases after LiveData class is updated.

  • Just pass the received data to the observer.
  • Modify LiveData and pass it to the observer
  • Get other LiveData with the help of updated LiveData and pass it to the observer.

The first case is simple and doesnot require any transformation so lets leave it for now.

Lets discuss about two other cases after updating LiveData.

So to address those two cases there are two types of transformation of LiveData.

  1. Transformations.map() : this will address our second cases we can modify our LiveData class and send another instance of modified LiveData to the observer.
  2. Transformations.switchMap(): lets discuss about the situation here.

If you are building the application where your login api provides token of the logined user and with the help of this token we have to fetch user data. So for like this case it comes handy. We can call another api with the help of previous fetched data and provide another LiveData instance to the observer because its meaningless to provide token to observer and create another LiveData observer in the same activity or fragment.

More or less i tried to give my all about new LiveData class. Now in next part we will be seeing LiveData into action soon. So stay tuned if you got something form this article. Second article link will be posted here. It contains simple example which will teach about view model and livedata.

--

--