Pandiyan Mani
2 min readAug 15, 2021

--

LiveData with ViewModel

What is LiveData?

LiveData is a holder class,which notify the active observer about the changes/modify over the data.LiveData aware of lifecycle,if you dont use live data and update a textview on screen rotation the data will lost on activity recreation incase of LiveData it will take care of it it will update with the recent data.

LiveData Just notify observer about changes it doesnt modify data.For that purpose we are using MutuableLiveData its is mutable as the name suggest it modify and notify the data

SetValue() and PostValue() is a public method in MutuableLiveData so it can be used with it.But in case of LiveData it is not public

SetValue() its used forupdateing the data on main thread we can also use postvalue() too for updating data in mian thread.

But if you want to update data from a background thread then we have to use postvalue() because setvalue() cannt do that since it support main thread operation.

MediatorLiveData is the subclass of MutuableLiveData it will add two LiveData we can remove and add LiveData.

Key Points we have to keep in mind?

1.Will LiveData notify obeserver on app minimize state?

No because on app minimize state the observer will be paused since its aware of lifecycle but once it resume back that data will be notified to the observer becuase it will be active.

2.Difference between LiveData Vs MutuableLiveData?

LiveData will just notify observer on data change but MutuableLiveData will modify and notify the changes.

MutuableLiveData is the subclass of LiveData.

What is ViewModel?

ViewModel holds UI Data,it will holds and restore UI data on activity recreation.

It act as a bridge between View and Model.

Example With LiveData,MutuableLiveData and ViewModel

ViewModel Class containg update of name

public class NameViewModel extends ViewModel {

MutableLiveData<String> mname = new MutableLiveData<>();

public void setName(String name)
{
mname.setValue(name);
}

public LiveData<String> getName()
{
return mname;
}

}

Listen changes on observer from LiveData

vm.getName().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
});

Intializing ViewModel

vm= ViewModelProviders.of(this).get(NameViewModel.class);

Updating Value to ViewModel

vm.setName("Hi");

Full code of Main Class that update name and display it on toast

public class Mainclass extends AppCompatActivity {

Button button_start;
NameViewModel vm;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_start=findViewById(R.id.button_start);
vm= ViewModelProviders.of(this).get(NameViewModel.class);
button_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vm.setName("Hi");
}
});
vm.getName().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
});

}

}

--

--