Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

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 withoutexecutePendingBindings()

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 withexecutePendingBindings()

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.

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

Always call it at the end of the onBindViewHolder

Solution 3:

It is like optimization advice. it forces the data binding to execute immediately, which allows the RecyclerView to make the correct view size measurements

Post a Comment for "Android Why/ When To Use Executependingbindings"