Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be stingy with "revalidate()" calls #3

Open
shannah opened this issue Jan 25, 2021 · 0 comments
Open

Be stingy with "revalidate()" calls #3

shannah opened this issue Jan 25, 2021 · 0 comments

Comments

@shannah
Copy link

shannah commented Jan 25, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant