Mallow's Blog

ANDROID ARCHITECTURE COMPONENT – LIVEDATA

INTRODUCTION:

Once again I welcome everyone to the series of Android Architecture Components. Last time we have seen about View Model and Room library. Now, we will be continuing with LiveData component, LiveData is an observable data holder class. Unlike other observable components, it is life cycle aware, meaning that it behaves according to the app lifecycle components (activity, fragment or service). One of the advantages is that there will be no memory leak issue while updating the app components and there will be always quick updated results.  

WORKING & BENEFITS:

LiveData is achieved by adding the lifecycle owner (i.e Activity and Fragment) as an observer to the data update. Following are some of the benefits :

– UI will always be up to date (UI showing most recent data), as activity and fragment will always be notified whenever there is a data change.

– As everything falls under lifecycle, registered for observing will only occur when the lifecycle owner is STARTED or RESUMED. So, that there will be no memory leaks or null pointer exceptions.

SAMPLE IMPLEMENTATION OF LIVE DATA:

Let’s understand with sample implementation:

1. First, add the necessary dependency:

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

Above two dependencies should be added in order to use to live data effectively, first one to add its implementation code and the second one to add its annotation related dependencies.

2. Consider we have a list of students to observe:

public class Students extends ViewModel {

    

    private MutableLiveData<List<Student>> studentLiveData = new MutableLiveData<>();

    

    public Students() {
         setStudentListRefreshCallback(newStudentList -> {
             studentLiveData.postValue(newStudentList);
         });
     }
 
     public LiveData<List<Student>> getStudentList() {
         return studentLiveData;
     }
 }

 

Here the list of students is liable to change so it is wrapped around the MutableLiveData. MutableLiveData is a subclass of LiveData that exposes the setValue and postValue methods so you can dispatch a value to any active observers. Whenever there are some changes in the list of students (maybe changes occurring in DB or from API calls) it will be intimated, it is done with help of live data. We can either use postValue() or getValue(). The postValue() method behaves in an asynchronous manner and getValue() in a synchronous manner.

3. Getting the updated values in Activity/Fragment:

public class StudentActivity extends AppCompatActivity {
     

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
        // initialising the view model
         Students studentsViewModel = ViewModelProviders.of(this).get(Students.class);
        // getting the updates and updating the list
         studentsViewModel. getStudentList ().observe(this, this::showStudents); 

    }
}

In above code we can see that observer() methods takes the first parameter as lifecyclerOwner (here activity) and the second one changes performed in the list which will be updated in the UI using showStudents() method.

CONCLUSION:
Thus livedata and ViewModel combined makes the life of Android developers easy by focusing on our core logic and not worrying about the data changes which occur in our local DB or through API call.

 

Featured Image courtesy: Thetechnocafe.com


Bringe Raj B,
Android Development Team,
Mallow Technologies.

 

 

 

Leave a Reply

%d bloggers like this: