简体   繁体   中英

Showing ProgressDialog only once in a recursive AsyncTask

In the Java code I have executed an AsyncTask class and with the returned result I have done a recursive call to itself in the onPostexecute method. Example:

MainActivity.java

public void button_clicked(){
  UploadAsync send_data = new UploadAsync(MainActivity.this);
  send_data.execute("send first data", user_data, file_path);
}

UploadAsync.java

@Override
protected String doInBackground(String... params) {
  String task = params[0];
  if(task.equals("send first data"){
    String user_data = params[1];
    String file_path = params[2];

    //send in the user_data to a php file

  }else if(task.equals("send file"){
    String file_path = params[1];

    //send the file_path to another php file

  }
}

@Override
protected void onPreExecute() {
 ProgressDialog pd = ProgressDialog.show(context, "Sending", "Please wait");
}

@Override
protected void onPostExecute(String result) {
  if(result.equals("data sent"){
    UploadAsync send_data = new UploadAsync(context);
    send_data.execute("send file", file_path);
  }else{
    //show error
  }

  pd.dismiss();
}

The code above is only an example made. Now the thing is, implementing this example will run the progress dialog twice. I have tried many ways to only show the progress dialog once while the AsyncTask is sending the user data and the file path but I'm not succeeding. Is there any suggestions on how to implement this correctly?

你能不能叫pd.dismiss()和解雇ProgressDialog当前UploadAsync执行新的前UploadAsync

In onPostExecute first, dismiss the dialog and then start new asynctask like this:

@Override
protected void onPostExecute(String result) {

  pd.dismiss();

  if(result.equals("data sent"){
    UploadAsync send_data = new UploadAsync(context);
    send_data.execute("send file", file_path);
  }else{
    //show error
  }

}

I hope this will help you.

Let ProgressDialog as global data .

private ProgressDialog pd;

@Override
protected void onPreExecute() {
    // show the ProgressDialog
    if (pd == null) {
        pd = ProgressDialog.show(context, "Sending", "Please wait");
    }
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("data sent") {
        UploadAsync send_data = new UploadAsync(context);
        send_data.execute("send file", file_path);
    }else{
        //show error
    }
    // edited here ,make the ProgressDialog dismiss
    if (pd.isShowing() && pd != null) {
        pd.dismiss();
        pd = null;
    }
}

In that case use callback mechanism between the async task and the caller(here, MainActivity ).

Implement as I explained below.

  • Define an interface with showProgressDiaglog() and DismissProgressDialog() methods.

  • MainActivity implements this interface and implements the two methods to show and dismiss progress dialog respectively.

  • Define setter method in your AsyncTask class and call this setter method from the MainActivity by passing this (this refers to MainActivity) after creating the AsyncTask instance and before calling execute method. store the passed interface implementation in the AsyncTask class as an instance variable.

  • Modify AsyncTask constructor to pass the task type instead of passing in the execute method and store this task type in the AsyncTask as an instance variable.

  • Now from within the AsyncTask constructor, in onPreExecute() method, call showProgressDialog() method based on the task type. and in onPostEecute() method call dismissProgressDialog() depending on the task type.

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