ProgressDialog.dismiss () not working

Please check the following code example. Toast messages are displayed, but the progress dialog is never hidden. Why?

import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Toast; public class LoadExamActivity extends Activity implements Runnable{ ProgressDialog pd; Handler Finished = new Handler(){ @Override public void handleMessage(Message msg){ Toast.makeText(getApplicationContext(), "DONE!", Toast.LENGTH_SHORT).show(); pd.dismiss(); } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.exam); Toast.makeText(this, "START!", Toast.LENGTH_SHORT).show(); pd = new ProgressDialog(this); pd.show(this, "Waiting...", "Please wait five seconds..."); Thread th = new Thread(this); th.start(); } public void run() { //To change body of implemented methods use File | Settings | File Templates. for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); }catch(Exception e){} } Finished.sendEmptyMessage(0); } } 

Five seconds later, the message β€œDONE” is displayed, but the progressdialog does not reject, and even if I put pd.dismiss () directly below thr pd.show (), I will not reject progressdialog either, and I do not know why this happens, and it drives me crazy!

+7
source share
1 answer

You are not using the right dialogue of progress. You will notice that the IDE displays a neat little warning sign next to your pd.show(...) .

What you do is

  • Create a dialog (invisible, irrelevant) with new ProgressDialog()

  • Create another dialog with the desired text using pd.Show() without saving a link to it.

  • Disable the first dialog. The dialogue from (2) remains.

If you replace your code with:

 //pd = new ProgressDialog(this); pd = ProgressDialog.show(this, "Waiting...", "Please wait five seconds..."); 

It should work fine.

+23
source

All Articles