. f Android Live Data Tutorial | Live Data | Data Binding | Architecture Components ~ Android Developers Blog

Saturday, 23 June 2018

Android Live Data Tutorial | Live Data | Data Binding | Architecture Components

Hello Friends,
              Today I am going to share me small tutorial of LiveData with Data Binding.

-What is LiveData?
     LiveData is an observable data holder class that can be observed within
     a given lifecycle. It lets the components in your app, usually the UI,
     observe LiveData objects for changes.

-The advantages of using LiveData

  •      Ensures your UI matches your data state
  •      No memory leaks
  •      No crashes due to stopped activities
  •      No more manual lifecycle handling
  •      Always up to date data
  •      Proper configuration changes
  •      Sharing resources
-How to use it in our app

   a. Add below dependency in app/build.gradle

  1. implementation "android.arch.lifecycle:extensions:1.1.1"   


   
   b. UserViewModel.java : Creating a ViewModel class

  1. package com.android.developer.livedatademo.model;  
  2.   
  3. import android.arch.lifecycle.MutableLiveData;  
  4. import android.arch.lifecycle.ViewModel;  
  5.   
  6. /** 
  7.  * Created by mukesh on 13/6/18. 
  8.  */  
  9. public class UserViewModel extends ViewModel {  
  10.   
  11.     // Create a LiveData with a String  
  12.     private MutableLiveData<user> mUser;  
  13.   
  14.     public MutableLiveData<user> getUser() {  
  15.         if (mUser == null) {  
  16.             mUser = new MutableLiveData<user>();  
  17.         }  
  18.         return mUser;  
  19.     }  
  20. }   
  21. </user></user></user>  

In Activity class we creates an observer which updates the ui.
  1. final Observer<user> nameObserver = new Observer<user>() {  
  2.       @Override  
  3.       public void onChanged(@Nullable final User user) {  
  4.       // Update the UI, in this case, a TextView.  
  5.       binding.tvUserName.setText(user.getName());  
  6.     }  
  7. };  
  8. </user></user>  

Next step is observe the livedata, passing in the activity as a LifecycleOwner and the Observer.
  1. userViewModel.getUser().observe(this, nameObserver);  
  2. binding.btnClick.setOnClickListener(this);   

The complete code,

  1. package com.android.developer.livedatademo;  
  2.   
  3. import android.arch.lifecycle.Observer;  
  4. import android.arch.lifecycle.ViewModelProviders;  
  5. import android.databinding.DataBindingUtil;  
  6. import android.support.annotation.Nullable;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10.   
  11. import com.android.developer.livedatademo.databinding.ActivityMainBinding;  
  12. import com.android.developer.livedatademo.model.User;  
  13. import com.android.developer.livedatademo.model.UserViewModel;  
  14.   
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener {  
  16.   
  17.     UserViewModel userViewModel;  
  18.     ActivityMainBinding binding;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
  23.         // Get the ViewModel.  
  24.         userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);  
  25.         // Create the observer which updates the UI.  
  26.         final Observer<user> nameObserver = new Observer<user>() {  
  27.             @Override  
  28.             public void onChanged(@Nullable final User user) {  
  29.                 // Update the UI, in this case, a TextView.  
  30.                 binding.tvUserName.setText(user.getName());  
  31.             }  
  32.         };  
  33.   
  34.         // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.  
  35.         userViewModel.getUser().observe(this, nameObserver);  
  36.         binding.btnClick.setOnClickListener(this);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         User user = new User();  
  42.         user.setName("Mukesh Yadav");  
  43.         user.setAge("25");  
  44.         userViewModel.getUser().setValue(user);  
  45.     }  
  46. }   
  47. </user></user>  

Download the complete code here

Hope this will helps someone.
Enjoy Coding... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.