Skip to content

Be stingy with "revalidate()" calls #3

@shannah

Description

@shannah

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions