What does postInvalidate() do?

AndroidAndroid LayoutInvalidation

Android Problem Overview


What is the use of the postInvalidate() function in Android? I have been seeing this function in a lot of places. When I Googled, I came out with this much:

> postInvalidate-- post an invalidate request on the UI-thread

I don't know what the phrase "invalidate request" there means. Can someone explain in detail what is happening here?

Android Solutions


Solution 1 - Android

Each class which is derived from the View class has the invalidate and the postInvalidate method. If invalidate gets called it tells the system that the current view has changed and it should be redrawn as soon as possible. As this method can only be called from your UIThread another method is needed for when you are not in the UIThread and still want to notify the system that your View has been changed. The postInvalidate method notifies the system from a non-UIThread and the View gets redrawn in the next eventloop on the UIThread as soon as possible. It is also briefly explained in the SDK documentation.

Just compare invalidate and postInvalidate.

Solution 2 - Android

By this method (postInvalidate()), you can invalidate a View from non-UI threads. To invalidate a View from UI-thread use invalidate() method.

The invalidation means, that the View.onDraw(Canvas) method will we called at some point in the future (not immediately) to redraw whole View (if the View is visible).

Android is redrawing Views automaticly, but sometimes is necessary to tell: "Hey, this View has been changed, redraw it as fast as possible."

More at:

http://developer.android.com/guide/topics/ui/how-android-draws.html

http://developer.android.com/reference/android/view/View.html#invalidate%28%29

Solution 3 - Android

In simple words, invalidate() method can be called from UI Thread and postInvalidate() can be called from non-UI Thread in order to tell android to update our Custom View after some changes are made to it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrogerstoneView Question on Stackoverflow
Solution 1 - AndroidMarioB.View Answer on Stackoverflow
Solution 2 - AndroidbranoholyView Answer on Stackoverflow
Solution 3 - AndroidRahul YadavView Answer on Stackoverflow