I donβt think there is any best practice recommended by android for this. I suggest you use an approach that uses cleaner and less boilerplate code.
If you use data binding to android files with LiveData , you can use the following approach:
Your POJO will look something like this.
public class User extends BaseObservable { private String firstName; private String lastName; @Bindable public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; notifyPropertyChanged(BR.firstName); } @Bindable public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; notifyPropertyChanged(BR.lastName); } }
So, you will already have a class that notifies you of every change to its property. That way, you can simply use this property change callback in your MutableLiveData to notify your observer. You can create a custom MutableLiveData for this
public class CustomMutableLiveData<T extends BaseObservable> extends MutableLiveData<T> { @Override public void setValue(T value) { super.setValue(value);
Then all you have to do is use this CustomMutableLiveData instead of MutableLiveData in your view model
public class InfoViewModel extends AndroidViewModel { CustomMutableLiveData<User> user = new CustomMutableLiveData<>(); ----- -----
Thus, by doing this, you can notify the viewer browser and LiveData with a slight change to the existing code. Hope this helps.
Abhishek v
source share