ProgressDialog not showing while performing a task
Asked Answered
P

3

11

I have a backup routine that copies everything from one folder to an external SD card which works perfectly. I'm trying to get an nice popup dialog box that shows when it's running but it just isn't showing. Doesn't even attempt to run (but the backup does complete).

Here's my code at the moment:

public void doBackup(View view) throws IOException{
    ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setMessage("Running backup. Do not unplug drive");
    pd.setIndeterminate(true);
    pd.setCancelable(false);
    pd.show();
    File source = new File("/mnt/extSdCard/DirectEnquiries"); 
    File dest = new File("/mnt/UsbDriveA/Backup");
    copyDirectory(source, dest);
    pd.dismiss();
}
Purely answered 15/10, 2012 at 13:58 Comment(2)
are you calling this method in onCreate()?????Beulahbeuthel
It's being called on a button pressPurely
B
41

You run long running tasks in a Thread or with an AsyncTask. Then your ProgressDialog will show up.

Do something like:

public void doBackup(View view) throws IOException{
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setMessage("Running backup. Do not unplug drive");
    pd.setIndeterminate(true);
    pd.setCancelable(false);
    pd.show();
    Thread mThread = new Thread() {
        @Override
        public void run() {
            File source = new File("/mnt/extSdCard/DirectEnquiries"); 
            File dest = new File("/mnt/UsbDriveA/Backup");
            copyDirectory(source, dest);
            pd.dismiss();
        }
    };
    mThread.start();
}
Beulahbeuthel answered 15/10, 2012 at 14:14 Comment(2)
can you explain why we should put pd.dismiss(); inside the thread I used mThread.join() and I put the pd.dismiss() after it put it doesn't work ?Bibbs
The showing and dismissing of the ProgressDialog object should be done with a handler, since only the original thread can touch the view.Gondar
N
2

Create a asyntask and put your time consuming tasks

 public void doBackup(View view) throws IOException{
        ProgressDialog pd = new ProgressDialog(this);
       pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setMessage("Running backup. Do not unplug drive");
       pd.setIndeterminate(true);
       pd.setCancelable(false);
        pd.show();
//create asyntask here
//Put below code in doInBackground mathod
        File source = new File("/mnt/extSdCard/DirectEnquiries"); 
        File dest = new File("/mnt/UsbDriveA/Backup");
        copyDirectory(source, dest);
//put this code in onPostExecute Method.
        pd.dismiss();
    }

You will get number of samples of Asyntask.

Neoarsphenamine answered 15/10, 2012 at 14:9 Comment(0)
A
1

Instead of doing this in one function at once. Do the following steps and it will definately work for you. 1. Create one async class.(It will create one separate thread for your copy directory functionality and wont run on main UI.) 2. Show your progress dialog before you execute the async class. 3. On post execute method, dismiss your dialog.

Adley answered 15/10, 2012 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.