I came across this code in a very basic Handler tutorial. The code is working fine but I do not understand why I have to use Handler for progressDialog.dismiss()
when an application starts, android system launch a process having a main thread, which is responsible to process UI Rendering, and Events. The Android UI isnot thread safe, so we can access android UI only by Event thread. In your program you have defined another thread than event by following code block:
new Thread() {
public void run() {
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
}
messageHandler.sendEmptyMessage(0);
}
}.start();
now if you want to dismiss progress dialog, you can do it only in event thread. Handler is used process/handle messages of a message queue. Handler associates with a thread, in your case its in event thread, as by default it would associate thread, in which it is being created. by messageHandler.sendEmptyMessage() another thread send a message to handler, and handler, process this message, in handleMessage method.