简体   繁体   中英

Progress Dialog not dismissing

In below code showing progress dialog with 5 second delay. After 5 second want to dismiss the dialog, but not happening in the below.

Can any one please help me where I made the mistake?

new CountDownTimer(5000, 1000) 
{
    public void onTick(long millisUntilFinished) 
    {
       // You don't need anything here
       dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
    }

    public void onFinish() 
    {
       dialog.dismiss();
    }

 }.start();

You are calling

dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);

in side onTick() method.

it will create new progress dialog instance at every tick. It might cause issue in your function and your dialog will not dismiss in this case.

Please try with below code

final ProgressDialog dialog = new ProgressDialog(DynamicView.this);
dialog.setTitle("Delay");
dialog.setMessage("Please wait...." + 5000 / 1000 + " Second(s)");
dialog.setIndeterminate(true);

new CountDownTimer(5000, 1000) {

    public void onTick(long millisUntilFinished) {
        // You don't need anything here
        dialog.setMessage("Please wait...." + millisUntilFinished / 1000 + " Second(s)");

        if (!dialog.isShowing())
            dialog.show();
    }

    public void onFinish() {
        if (dialog.isShowing())
            dialog.dismiss();
    }
}.start();

This code will works same as you need.

Instead of assign value of Progress Dialog to dialog variable. Create Progress Dialog object and set title to it as below.

ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("what ever message you like");
pd.show();

in onFinish() please call pd.dismiss();

Please set the message in onTick method and show progress dialog in this method if progress dialog is not already shown.

ProgressDialog pd = new ProgressDialog(yourActivity.this);
new CountDownTimer(5000, 1000) 
{
    public void onTick(long millisUntilFinished) 
    {
       // You don't need anything here
       pd.setMessage("what ever message you like");
        if (!pd.isShowing())
            pd.show();
    }

    public void onFinish() 
    {
       pd.dismiss();
    }

 }.start();

on tick is calling multiple time that's why your dialog is not closing

implement this

new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
                // You don't need anything here
                if(!dialog.isShowing())
                {
                    dialog= ProgressDialog.show(mContext,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
                }
            }
            public void onFinish() {
                dialog.dismiss();
            }
        }.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM