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()
First: Let us know what is thread:
Second: Let us know about the application thread:-
Android UI-Toolkit is not thread safe
Handler class:
android.os.Handler
Instance of handler is made
Handler handlerObject = new Handler();
Final piece on using handler is to use Runnable Interface:
Class NameOfClass implements Runnable
{
Public void run()
{
//Body of run method
}
}
Putting all together
//Create handler in the thread it should be associated with
//in this case the UI thread
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
while(running){
//Do time consuming stuff
//The handler schedules the new runnable on the UI thread
handler.post(new Runnable() {
//Ex.. using progressbar to set the pogress
//Updating the UI is done inside the Handler
});
}
}
};
new Thread(runnable).start();