Tuesday 6 November 2018

Android Architecture Components : Live Data

Hi ,

In my previous post , I have described about ViewModel , and in this post I will explain about Live Data .Below are some properties of Live Data :


  •  Made it easy to update the UI data
  •  Observable Data holder class
  •  We need to observe Live Data , from the app's component's in onCreate()
  •  Keeps the UI updated , if value has been change of the data
  •  Automatically destroyed , when the associated LifeCycle Owner has been destroyed
  •  During data fetching from n/w or DB ,No crashes due to stop activity
  •  Can be shared by multiple resources
  •  Case:1 when the activity is in pause /stop state the activity will stop to observe the LiveData
  •  Case:2 when the activity is destroyed , it will stop to oberver the LiveData
  •  LiveData will only be observed by the activity , when it is in active state


So Live Data is a simple Data Type , having the capability to notify its observer when its value has been changed.
Live Data only notify the observer , when activity is in active state , so it prevents many crashes due to network call etc.

Before start let's take a look on- 

Mutable Data : Since Live Data , does not provide any public method to updated the Stored Data , we used its sub class,
this class provides us 2 methods:
  1. postValue()
  2. setValue()
setValue() method must be called from the main thread. 
But if you need set a value from a background thread, postValue() should be used.

Now let's take a look on Live Data simple demo , after this post I will explain demo for ViewModel+Live Data , as Live Data is best suited with view model.

So here I am taking one layout with one button and one text-view , on clicking that button , we are showing some random number on text-view , by using Live Data.

The layout which I am creating is named as : live_data_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/text_live_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random"/>

</LinearLayout>


Now check the java file for activity class named as : LiveDataDemoActivity.java

package com.development.live_data;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.development.R;

import java.util.Random;

public class LiveDataDemoActivity extends AppCompatActivity {
private MutableLiveData mMutableLiveData;
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.live_data_layout);
mTextView = (TextView) findViewById(R.id.text_live_data);
mButton = (Button) findViewById(R.id.button);
mMutableLiveData = new MutableLiveData<>();

/**
* Observe the random number
*/
mMutableLiveData.observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
mTextView.setText(s);
}
});

/**
* Click on button
*/
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
mMutableLiveData.setValue("Random Int: " + random.nextInt());
}
});
}
}

Here you can see, that on click on the button , I am setting the data using Mutable Live Data setValue() , not using setText() of Text-view . Run the code and enjoy.

Benefit of using this method is , it will only update the text-view when activity is in active mode , otherwise not . So it is very useful to prevent memory leakage , application crashing etc.

 That's it .Happy Coding :)





No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...