ProgressDialog.dismiss() is not working
Asked Answered
H

2

9

Please check the following sample code. Toast messages are shown but the progressdialog 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);
    }


}

After five seconds the "DONE" message is shown but the progressdialog is not dismissed and even if I put pd.dismiss() right below thr pd.show() I wont dismiss the progressdialog either and I don't know why this is happening and it's driving me crazy!

Handlebar answered 25/2, 2012 at 13:45 Comment(0)
S
31

You are not using the progress dialog right. You'll notice the IDE shows a neat little warning sign next to your pd.show(...) line.

What you are doing is

  1. Create an (invisible, irrelevant) progress dialog using new ProgressDialog()

  2. Create another progress dialog with the desired text using pd.Show(), without storing a reference to it.

  3. Dismiss the first dialog. The dialog from (2) remains.

If you replace your code with:

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

it should run just fine.

Seaton answered 25/2, 2012 at 13:54 Comment(2)
It WORKED! You were right about the Warning and I didn't noticed it. But I cannot understand why does the IDE tell me that the .show() method does not return anything (void) and after all it returns an instance of a ProgressDialog?Handlebar
Yes, this works. show() returns an instance of ProgressDialog but using the new keyword also, we get the same instance, I hope. Using the above way, the dismiss code worked, on the other hand, using the new keyword, the code was not working.Masterson
C
0

Problem here is with the context which you have used to create the ProgressDialog. Use main activity context to create the ProgressDialog and use show() and dissmiss() method where you need.

Casein answered 11/5, 2018 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.