4

I use a Service to upload files. While a file is being uploaded, I display a ProgressDialog.

When I rotate the screen the ProgressDialog dismisses.

How can I keep the ProgressDialog around?

I'd rather not override onConfigurationChanged.

Thanks!

benkdev
  • 673
  • 2
  • 16
  • 32

2 Answers2

3

Android should handle this automatically if you show your dialog with the showDialog(int) method.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • 2
    Awesome, thanks! When my activity gets destroyed do I need to call showDialog from onCreate or will the activity automatically show the dialog again until I call dismiss? – benkdev Nov 20 '11 at 05:30
1

Expanding on Dalmas' suggestion, if you happened to be like me, where you created the dialog in a separate class for managing the async task, then one line might be all you need to add:

mProgressDialog.setOwnerActivity((Activity)mContext);

This allows the Activity to manage the dialog. A longer snippet for reference...

public DownloadCatalog(Context c) {
    mContext = c;
}

public void startDownload() {
    mProgressDialog = new ProgressDialog(mContext);
    mProgressDialog.setOwnerActivity((Activity)mContext);
    mProgressDialog.setMessage("Downloading \n" + remoteDirectory);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setCancelable(false);


    new DownloadFileAsync().execute(fileUrl);
}


class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show(); 
    }
Frank Yin
  • 1,920
  • 1
  • 17
  • 12