Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android data binding : view does not update when property is changed

People also ask

Which is better view binding or data binding in Android?

View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.

Is data binding deprecated Android?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.

Is data binding good in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.


In case you might be experiencing this issue with LiveData, you might've forgotten to set lifecycle owner of your binding; binding.setLifecycleOwner(lifecycleOwner)


I had a similar issue when I needed to refresh the views after an event, with my variable objects not observable, so I added this:

binding?.invalidateAll()

I hope it helps.


You need to call executePendingBindings() for immediately update binding value in view:

When a variable or observable changes, the binding will be scheduled to change before the next frame. There are times, however, when binding must be executed immediately. To force execution, use the executePendingBindings() method.

Look at Advanced Binding chapter for more info


  • Make your model class fields private.
  • Change getCalcResult() should be public.
  • BaseView should be extending BaseObservable
  • If you don't change model value dynamically. Then you need not to make every accessor @Bindable. You can remove that. And also remove notifyPropertyChanged(). Opposite of this if you need to change model values dynamically then use @Bindable attribute.

You are good to go.

public class BaseCalcModel extends BaseObservable {
private String calcResult;
private BaseView firstNumView;
private BaseView secondNumView;
private BaseView resultNumView;
private int firstNumBase;
private int secondNumBase;
private int resultNumBase;
private String error;

public String getCalcResult() {
    return calcResult;
}

public void setCalcResult(String calcResult) {
    this.calcResult = calcResult;
}

public BaseView getFirstNumView() {
    return firstNumView;
}

public void setFirstNumView(BaseView firstNumView) {
    this.firstNumView = firstNumView;
}

public BaseView getSecondNumView() {
    return secondNumView;
}

public void setSecondNumView(BaseView secondNumView) {
    this.secondNumView = secondNumView;
}

public BaseView getResultNumView() {
    return resultNumView;
}

public void setResultNumView(BaseView resultNumView) {
    this.resultNumView = resultNumView;
}

public int getFirstNumBase() {
    return firstNumBase;
}

public void setFirstNumBase(int firstNumBase) {
    this.firstNumBase = firstNumBase;
}

public int getSecondNumBase() {
    return secondNumBase;
}

public void setSecondNumBase(int secondNumBase) {
    this.secondNumBase = secondNumBase;
}

public int getResultNumBase() {
    return resultNumBase;
}

public void setResultNumBase(int resultNumBase) {
    this.resultNumBase = resultNumBase;
}

public String getError() {
    return error;
}

public void setError(String error) {
    this.error = error;
}

}


I had the exact same problem. Apparently it was a single line that had been causing the issue. All you have to do is set the licycleowner to the binding.

binding.lifecycleOwner = this

Please make sure you are updating the same object field when data binding does not work on you. The object's instance must be the same in order to update UI. Maybe my answer helps to someone, enjoy coding!