Search code examples
androidview

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


Solution

  • 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.