Trevor
Trevor

Reputation: 10993

Detecting when a View has been invalidated

In a custom View, I need to perform some additional work inside onDraw() if and only if the View was invalidated by the application; that is, my own code called invalidate() in the UI thread or postInvalidate() in a non-UI thread. If on the other hand onDraw() is being called because the system invalidated the View, I don't wish that additional work to be performed.

What's the best way to achieve this? My immediate thought is to simply override invalidate() and postInvalidate() and set a flag in both of those, but it would be nicer if there was a single UI-thread method I could override.

Any thoughts please?

Thanks, Trev

Upvotes: 1

Views: 1320

Answers (2)

Jan Rozenbajgier
Jan Rozenbajgier

Reputation: 644

There is a way to do that without extending view class.

view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
        @Override
        public void onDraw() {
            //View was invalidated
        }
    });

Upvotes: 0

Romain Guy
Romain Guy

Reputation: 98521

postInvalidate() ends up calling invalidate() so you don't need to override both. But if you override invalidate(), the system will call the overridden version.

Upvotes: 4

Related Questions