Two Android Progressdialog one after the other

Asked

Viewed 239 times

0

How do I display two Preview Dialog, when one is finished, open the other?

In my case the user clicks a button, then several files of a folder are moved and renamed to another folder, it first sends everything, then renames. Then I would have to have 2 Progress dialogs, one for when sending, and the other for when renaming. But only appear when the other disappears...

I need Progress dialog to rename as there are more than 200 files.

I researched and it seems that it is not possible. Some idea?

  • 2

    The ideal would not be a bar Progress showing the steps it performs?

  • 2

    I find the idea of @Reiksiel very cool, even thinking about UX. I think that after the first dialog the user does not expect another hehe. Try to use one DialogFragment, in it you can customize the View and put a TextView with the stages.

  • I hadn’t thought about it... But there’s no way to change the title and message of the Dialog within the thread? I would need to change them too, tried but just doesn’t change...

  • @Wakim now got good, I was able to do... Give me an answer showing an example of how to customize the view in this part, that I choose the answer

  • @Felipe.rce, you already have an answer with this idea. If it wasn’t as you implemented it, you can write the answer and accept it. Staying as information for the next who come to have this doubt.

2 answers

0

The way you put it was very generic, but you can put at the end of the 1 thread ( sending files) call the Dismiss() of the 1 Progressdialog, and soon after call the 2 thread (rename files) this thread will call the 2 Progressdialog and at the end call the Dismiss() of the same.

0


Do it like this:

public static class OperationFiles extends AsyncTask<Void, Integer,Void>
    {
    public enum Operation
    {
      Move,
      Rename
    }

    private ProgressDialog progressDialog;
    private Context context;
    private OnPostExecuteListener onPostExecuteListener;
    private List<File> filesList;
    private String destino;
    private Operation operation;


    public OnPostExecuteListener getOnPostExecuteListener() {
        return onPostExecuteListener;
    }

    public void setOnPostExecuteListener(OnPostExecuteListener onPostExecuteListener) {
        this.onPostExecuteListener = onPostExecuteListener;
    }

    //aqui você pode receber a lista de arquivos e o novo local
    public OperationFiles(Context context,List<File> filesList, String destino, Operation operation)
    {
        this.context = context;
        this.progressDialog = new ProgressDialog(context);
        this.filesList = filesList;
        this.destino = destino;
        this.operation = operation;
    }


    @Override
    public void onPreExecute()
    {
        if(operation == Operation.Move)
            this.progressDialog.setMessage("Movendo os arquivos...");
        else
            this.progressDialog.setMessage("Renomeando os arquivos...");

        this.progressDialog.setMax(filesList.size());
        this.progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids)
    {

        for(int i = 0; i < filesList.size(); i++)
        {
            //faz a operação para defina no construtor aqui
            if(operation == Operation.Move)
            {

            }
            else
            {

            }

            //itera no progresso
            publishProgress(i);
        }

        return null;
    }


    @Override
    public void onProgressUpdate(Integer... progress)
    {
        this.progressDialog.setProgress(progress[0]);
    }


    @Override
    public void onPostExecute(Void result)
    {
        //dismiss do diálogo
        this.progressDialog.dismiss();

        //chama o callback informando o término do processo
        if(getOnPostExecuteListener() != null)
            getOnPostExecuteListener().onPostExecute();
    }

    //Irá fazer um callback para quando o processo terminar
    public interface OnPostExecuteListener{
        public void onPostExecute();
    }
}

Consider that this is an example and you can change according to your problem. So I did, as soon as one operation is completed, he calls the other, making two different dialogues run.

The use would look like this:

 //Faz a operacao de mover os arquivos
        OperationFiles operationFiles = new OperationFiles(this, new ArrayList<File>(), "/sdcard...", OperationFiles.Operation.Move );

    //informa o callback
    operationFiles.setOnPostExecuteListener(new OperationFiles.OnPostExecuteListener()
    {
        @Override
        public void onPostExecute()
        {
            ///aqui a operacao de renomear
            OperationFiles operationFilesRename = new OperationFiles(Main.this, new ArrayList<File>(), "/sdcard...", OperationFiles.Operation.Rename);
            operationFilesRename.execute();
        }
    });

    operationFiles.execute();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.