UberEatsClone icon indicating copy to clipboard operation
UberEatsClone copied to clipboard

Be stingy with "revalidate()" calls

Open shannah opened this issue 5 years ago • 0 comments

The update() method on a view may be called a lot, including circumstances when the state of the view is already fine. Try to avoid UI mutations or expensive calls like revalidate() unless required.

E.g. In DishAddOnView.java's update method we have:

@Override
    public void update() {
        if (isSelected){
            getStyle().setBgTransparency(255);
        }else{
            getStyle().setBgTransparency(0);
        }
        revalidate();
    }

I would guard against both the setBgTransparency() calls and the revalidate, as follows:

@Override
    public void update() {
        int bgTransparency = getStyle().getBgTransparency();
        if (isSelected && bgTransparency != 255){
            getStyle().setBgTransparency(255);
            revalidateWithAnimationSafety();
        }else if (!isSelected && bgTransparency != 0){
            getStyle().setBgTransparency(0);
            revalidateWithAnimationSafety();
        }

    }

Note: Also prefer revalidateWithAnimationSafety() to revalidate() in almost all cases. The only exception is when you are calling revalidate() to update layout and you need to access the updated layout coordinates immediately. The difference is that, if there is an animation in progress, revalidate() may cause it to be janky as it will interrupt the transitions.

shannah avatar Jan 25 '21 13:01 shannah