简体   繁体   中英

Android ProgressDialog not showing (blocked by code in other Thread)

Looking to similar questions everybody solve the problem of not appearing their Progress Dialog putting the intermediate code in a separate Thread.

My problem is that the mentioned solution is not working for me.

In my activity:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.dialog_ddbb_download_text)
                .setPositiveButton(R.string.Accept, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // In this method I show the progress dialog
                        showProgressAndDownloadDDBB();
                    }
                })
                .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        builder.create().show();

Method in the activity:

private void showProgressAndDownloadDDBB() {
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate(true);
    progressDialog.show();
    // Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
    DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
    ddbb_download_manager.run();
}

My runnable class, expected to run the intermediate code in a separate Thread:

public class DDBB_Download_Manager implements Runnable {


public DDBB_Download_Manager(Context context, ProgressDialog progressDialog) {
    this.mContext = context;
    this.mProgresDialog = progressDialog;
}

@Override
public void run() {
    someCode()
    Thread.sleep(3000);
    // The GUI shows the accept Button clicked for 3 seconds (like it was freezed)
    // Here I try to hide the Progress dialog after finishing the job, but it doesn't matter becasuse the progress dialog didn't even show up.
    View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
    rootView.post(new Runnable() {
        @Override
        public void run() {
            mProgresDialog.dismiss();
        }
    });
}

So the question is: if I am executing the code between the Show and Dismiss methods of the Progress Dialog in a different Thread than the UI Thread, why is not the dialog showing up?

In fact it appears If I don't call the Runnable.

That is because you are running directly the dismiss() method from the runnable when you call ddbb_download_manager.run() where the progress dialog is cleared/done and if you are not calling it then the progress dialog will show due to that dismiss is on yet been called.

Make sure that you call ddbb_download_manager.run() when you want your progress dialog to be dismissed. don't call it directly after you show your progress dialog.

private void showProgressAndDownloadDDBB() {
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate(true);
    progressDialog.show();
    // Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
    DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
    Handler handler = new Handler();
    handler.postDelayed(ddbb_download_manager ,3*1000);
}

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