ProgressDialog not showing up in activity

这一生的挚爱 提交于 2019-11-28 05:47:38

问题


I am trying to include a ProgressDialog in my application. But it is not showing up.

Here's the code snippet where i use the ProgressDialog:

public class abcActivity extends Activity {
    public boolean onOptionsItemSelected(MenuItem item) {
        case XYZ:
            ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
            callSomeFunction();
            dialog.dismiss();
            showToast(getString(R.string.SomeString));
            break;
    }
}

Does anyone know why the dialog is not showing up? Any clues?


回答1:


I think your code is wrong in a sense that you do all in the UI thread. You have to put callsomefunction() into a background thread.

public void runSomething()
{
    showDialog(BACKGROUND_ID);
    Thread t = new Thread(new Runnable() 
    {                   
        public void run() 
        {
            //do something
            handler.post(finishThread);
        }
    });

    t.start();
    // The progress wheel will only show up once all code coming here has been executed
}

And as well

protected Dialog onCreateDialog(int id)
{
    if(progressDialog == null) progressDialog = new ProgressDialog(this);
    return progressDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
    if(id == BACKGROUND_ID) 
    {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("running for long ...");
    }
}

Runnable finishThread = new Runnable()
{       
    public void run() 
    {
//long running
        if(progressDialog != null) progressDialog.dismiss();
    }
};



回答2:


i think issue is in your switch condition pls verify it.

here is another method to display dialog in android try this.

public static final int DIALOG2_KEY = 1;
public static ProgressDialog dialog1;

showDialog(DIALOG2_KEY); // use it where you want to display dialog 

dialog1.cancel();  // use it to cancel the dialog


@Override
    public Dialog onCreateDialog(int id) {
        if (id == 1) {
            dialog1 = new ProgressDialog(this);
            dialog1.setMessage("Please wait...");
            dialog1.setIndeterminate(true);
            dialog1.setCancelable(true);
        }
        return dialog1;
    }



回答3:


Ensure you have this code called by debugging or adding a log. The code seems right to me.

Also, if you want to perform some operations in background and show a progress dialog while the performing, please use AsyncTask with ProgressDialog bounded, like here.




回答4:


ProgressDialog is deprecated in Android O , Use ProgressBar now.



来源:https://stackoverflow.com/questions/4861710/progressdialog-not-showing-up-in-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!