0

I have a class that manages the download/upload of files to a remote service on the cloud. This class implements a listener which notifies when an operation has been finished (either a file upload/download is done, or a set of files is uploaded/downloaded).

There is a ProgressDialog created to show the progress of the operation. The progress bar is populated when the listener gets called by the download/upload manager.

The problem comes when the user turns the screen. In these case I could save the current status of the progress bar, but how can I keep the reference to the upload/download listener?

EDIT: When the user turns the screen, the progress bar is gone because the UI in the activity gets reloaded. The download/upload process continues normally until it finishes and the method "progressBar.dismiss()" is called. Then comes an exception, because the progress bar doesn't exist anymore.

SOLUTION: Follow this link to get a solution using Fragments.

UploadDownloadListener.java

public interface UploadDownloadListener {

   void operationFinished(File file);

   void operationFinished();
}

UploadDownloadManager.java

public void downloadAll(final UploadDownloadListener uploadDownloadListener) {
   ... on file downloaded ...
   uploadDownloadListener.operationFinished(file);

   ... on all files downloaded ...
   uploadDownloadListener.operationFinished();
}

MyActivity.java

public class MyActivity extends FragmentActivity {
   private ProgressDialog progressBar;
   private Handler progressBarbHandler = new Handler();

   ...
   private void createAndShowProgressDialog(View view, int numberOfFiles, CharSequence title) {
       Log.i(TAG, "Create progress dialog. title [" + title + "] max[" + numberOfFiles + "]");
       progressBar = new ProgressDialog(view.getContext());
       progressBar.setCancelable(false);
       progressBar.setMessage(title);
       progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
       progressBar.setProgress(0);
       progressBar.setMax(numberOfFiles);
       progressBar.show();
   }

   private class ButtonDownloadOnClickListener implements View.OnClickListener {
      @Override
      public void onClick(View view) {
        final int numberOfFiles = getFileToDownload.size();
        createAndShowProgressDialog(view, numberOfFiles, getResources().getText(R.string.label_download));
        new Thread(new Runnable() {
            @Override
            public void run() {
                uploadDownloadManager.uploadAll(getFileToDownload(), getUploadDownloadListener());
            }
        }).start();
    }

    private UploadDownloadListener getUploadDownloadListener() {
      return new UploadDownloadListener() {
        int item = 0;

        @Override
        public void operationFinished(File file) {
            item++;
            progressBarbHandler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(item);
                }
            });
        }

        @Override
        public void operationFinished() {
            sleep(2000);
            progressBar.dismiss();
            returnToMainActivity();
        }
    };
}
Community
  • 1
  • 1
ado
  • 1
  • 3
  • You can use the AsyncTask for knowing the state and relaunch/not launch again. AsyncTask has methods to know whether the task has got Completed or yet be finished. If task is still running then you need not create the task. This may help you. – kumar Dec 03 '13 at 08:00
  • @kumar the main problem here is how to re-create the progress dialog in the onCreate method. the background task will call back the progress dialog method "setProgress" and "dismiss", but in the case of turning the screen, the dialog does not exist anymore. – ado Dec 04 '13 at 05:49

1 Answers1

0

the way i'm solving a problem like this is to make the whole class asyncTask and then make a public variable in the ui class and update the ui from the post, pre and on progress update classes in the asyncTask class.

Ivan
  • 4,186
  • 5
  • 39
  • 72
  • keep in mind that on turning the screen the progress dialog will disappear. how can i create it again in a way that the background task can reach it? – ado Dec 04 '13 at 05:51
  • hmm after researching this tropic i came up with no good answer and realized i had a simuler problem with my app :( found this link but not sour if it works http://stackoverflow.com/questions/5465422/how-to-show-dialogs-in-android-taking-screen-orientation-changes-into-account –  Dec 04 '13 at 07:28
  • I just found a solution for this situation. Got the idea from [here](http://stackoverflow.com/a/16305029/2823390). So basically the idea is to create a Fragment to do the Async task and use FragmentManager to find the Fragment back. I tested it already and it works fine. – ado Dec 05 '13 at 18:47