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
a. Add below dependency in app/build.gradle
b. UserViewModel.java : Creating a ViewModel class
In Activity class we creates an observer which updates the ui.
Next step is observe the livedata, passing in the activity as a LifecycleOwner and the Observer.
The complete code,
Download the complete code here
Hope this will helps someone.
Enjoy Coding... :)
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
implementation "android.arch.lifecycle:extensions:1.1.1"
b. UserViewModel.java : Creating a ViewModel class
package com.android.developer.livedatademo.model;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
/**
* Created by mukesh on 13/6/18.
*/
public class UserViewModel extends ViewModel {
// Create a LiveData with a String
private MutableLiveData mUser;
public MutableLiveData getUser() {
if (mUser == null) {
mUser = new MutableLiveData();
}
return mUser;
}
}
In Activity class we creates an observer which updates the ui.
final ObservernameObserver = new Observer () { @Override public void onChanged(@Nullable final User user) { // Update the UI, in this case, a TextView. binding.tvUserName.setText(user.getName()); } };
Next step is observe the livedata, passing in the activity as a LifecycleOwner and the Observer.
userViewModel.getUser().observe(this, nameObserver); binding.btnClick.setOnClickListener(this);
The complete code,
package com.android.developer.livedatademo;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.android.developer.livedatademo.databinding.ActivityMainBinding;
import com.android.developer.livedatademo.model.User;
import com.android.developer.livedatademo.model.UserViewModel;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
UserViewModel userViewModel;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// Get the ViewModel.
userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
// Create the observer which updates the UI.
final Observer nameObserver = new Observer() {
@Override
public void onChanged(@Nullable final User user) {
// Update the UI, in this case, a TextView.
binding.tvUserName.setText(user.getName());
}
};
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
userViewModel.getUser().observe(this, nameObserver);
binding.btnClick.setOnClickListener(this);
}
@Override
public void onClick(View v) {
User user = new User();
user.setName("Mukesh Yadav");
user.setAge("25");
userViewModel.getUser().setValue(user);
}
}
Download the complete code here
Hope this will helps someone.
Enjoy Coding... :)