The difference between invalidate () and Postinvalidate () in Android and how to use it _android

Source: Internet
Author: User

There are two sets of methods for implementing view updates in Android, one for invalidate and the other for Postinvalidate, which is used in the UI thread itself and the latter in non-UI threads.

Android provides a invalidate method for interface refreshes, but invalidate cannot be invoked directly on the thread because he violates the Single-threaded model: Android UI operations are not thread-safe and must be invoked in the UI thread.

Invalidate () is used to refresh view, and must work in the UI thread. For example, when you modify the display of a view, call invalidate () to see the redrawn interface. The call to Invalidate () POPs the old view from the main UI thread queue. An Android program has only one process by default, but it can have many threads under one process.

Among so many threads, threads that are primarily responsible for controlling the display, update, and control interaction of the UI interface are called UI threads, and because the OnCreate () method is performed by the UI thread, the UI thread can also be understood as the main thread. The rest of the threads can be understood as worker threads.

Invalidate () has to be mobilized in the UI thread, and the UI thread can be notified by the handler to interface updates by the worker thread, while Postinvalidate () is invoked on the worker thread.

Refreshing the interface with invalidate ()

Instantiates a handler object and overrides the Handlemessage method call invalidate () to implement the interface refresh, while the message is updated in the thread through the SendMessage send interface.

In OnCreate (), Start thread new thread (new Gamethread ()). Start (),//Instantiate a Handler Handler MyHandler = new Handler () {//Receive Handles the public void Handlemessage (msg) {switch (msg.what) {case Activity01.REFRESH:mGameView.invalidate () after the message. 
/refresh interface break; 
Super. Handlemessage (msg); 
} 
}; Class Gamethread implements Runnable {public void run () {while! 
Thread.CurrentThread (). isinterrupted ()) {message: = new Message (); 
Message.what = Activity01.refresh; 
Send messages Activity01.this. myhandler.sendmessage (message); 
try {thread.sleep (100); 
The catch (Interruptedexception e) {thread.currentthread (). interrupt (); 
The new Thread (new Gamethread ()) is opened in OnCreate (). Start (),//Instantiate a Handler Handler MyHandler = new Handler () {  Handles the public void Handlemessage (msg) {switch (msg.what) {case Activity01.REFRESH:mGameView.invalidate () after receiving the message;
Refresh interface break;
Super.handlemessage (msg);
}
}; Class Gamethread implements Runnable {public void run () {while (! Thread.CurrentThread (). isinterrupted ()) {Message messages = new (); message.what = Activity01.refresh;
Vity01.this.myHandler.sendMessage (message);
try {thread.sleep;} catch (Interruptedexception e) {thread.currentthread (). interrupt ();}} }
}

Use Postinvalidate () to refresh the interface

The use of postinvalidate is relatively simple, do not need to handler, directly in the thread call Postinvalidate can be.

Class Gamethread implements Runnable {public 
void run () {while 
! Thread.CurrentThread (). isinterrupted ()) { 
try { 
thread.sleep () 
} catch (Interruptedexception e) { 
Thread.CurrentThread (). interrupt (); 
} 
Using Postinvalidate, you can update the interface mgameview.postinvalidate () on the thread directly 
();} 

The above is a small set for everyone to share the Android invalidate () and postinvalidate () and the difference between the use of the method, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.