Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog.dismiss() is not working

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!

like image 591
Davidoff Avatar asked Feb 25 '12 13:02

Davidoff


People also ask

How do I dismiss ProgressDialog in flutter?

as per your source code you use https://pub.dev/packages/progress_dialog package for display progress dialog. so as per this package for dismiss dialog you need to use the below method. await pr. hide();

Why is ProgressDialog deprecated?

ProgressDialog 's look can be replicated by placing a ProgressBar into an AlertDialog . You can still use it, but Android does not want you to use it, that is why it is deprecated.

How to use ProgressDialog in android?

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this);

What is ProgressDialog?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.


1 Answers

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.

like image 108
Paul-Jan Avatar answered Nov 09 '22 01:11

Paul-Jan