-1

I am not very clear functioning of the 'AsyncTask.

I'm trying to put a button in the ProgressDialog to cancel AsynkTask.

The problem is that when I invoke the method: runner.cancel (true); It seems that the ProgressDialog disappears. But asynkTask continues to work in the background.

I show my code:

public class AsyncTaskRunner extends AsyncTask<String, String, String> {
        @Override
        protected void onCancelled(String result) {
            pDialog.dismiss();
            super.onCancelled(result);
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(context);

            pDialog.setCancelable(false);
            pDialog.setMessage(context.getResources().getString(
                    R.string.pDialog));
            if (codeLink == 2) {
                pDialog.setButton("cancel", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        runner.cancel(true);

                    }
                });
            }

            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
        // Here download the data.
        }
        @Override
        protected void onPostExecute(String result) {
        //Here I make the parser.
        }
}

My guess:

it may be that doing it this way gate doInBackground () but OnPostExecute () is executed? if it were alkoxy how do I erase everything? Also OnPostExecute () ??

uynva
  • 97
  • 1
  • 16

1 Answers1

0

AsyncTask works in background. First onPreExecute is called then doInBackground and then after completing the background task, onPreExecute is called.

just keep checking isCancelled() in the doInBackground.

 protected Object doInBackground(Object... x) {
    while (/* condition */) {
      // work...
      if (isCancelled()) break;
    }
    return null;
 }
Atif Farrukh
  • 2,219
  • 2
  • 25
  • 47