Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Why/ when to use ExecutePendingBindings

Lately I've been using data bindings and I came across the executePendingBindings method.

The documentation doesn't say much about it, so I don't understand how it works or when to use it. Here is an example of the method's usage.

Please give an example that demonstrates the difference of using it and not. Thank you

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {    
    Customer customer= List.get(position).second;
    
    ((CustomerViewHolder)holder).binding.setCustomer (customer)
    ((CustomerViewHolder)holder).binding.executePendingBindings();
    
}
like image 846
Nick Avatar asked Sep 12 '25 18:09

Nick


2 Answers

For a more detailed explanation you can refer to this example.

When not using executePendingBindings() your list will flicker when you bind it, for example when you open a new list you will notice this flicker / jitter effect, this is because the list is populated and then in the next frame it's bound.

If you don't want this to happen and execute immediately, your binding this method will prevent the flickering and bind your data right away.

Here is an example without executePendingBindings()

enter image description here

If you see, there is a flickering effect since the views are initialized, then data binded and then in the next frame the view binding happens.

Here is with executePendingBindings()

enter image description here

If you see here, there is no flickering effect, you can see that the list is binded normally and works as one go.

You can only use executePendingBindings() on the UI thread, this means that in the time onBindViewHolder is called, you will need to use it on the binding, doing so will guarantee that you are calling it on the UI.

override fun onBindViewHolder(binding: MyBindingClass, position: Int, viewType: Int) {
        //Your binding code
        binding.executePendingBindings()
    }

Always call it at the end of the onBindViewHolder

like image 76
Gastón Saillén Avatar answered Sep 15 '25 08:09

Gastón Saillén


executePendingBindings() function is for immediate binding.

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

like image 23
Rajat Beck Avatar answered Sep 15 '25 07:09

Rajat Beck