Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy use Handler?
    text
    copied!<p>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 <code>progressDialog.dismiss()</code> ??? I removed the Handler part and placed <code>progressDialog.dismiss()</code> in the <code>run()</code> method and it worked fine. So why used Handler???</p> <pre><code> import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class HandlerThread extends Activity{ private Button start; private ProgressDialog progressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button) findViewById(R.id.Button01); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub fetchData(); } }); } protected void fetchData() { // TODO Auto-generated method stub progressDialog = ProgressDialog.show(this, "", "Doing..."); new Thread() { public void run() { try { Thread.sleep(8000); } catch (InterruptedException e) { } messageHandler.sendEmptyMessage(0); } }.start(); } private Handler messageHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); progressDialog.dismiss(); } }; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload