1

AsyncTask still runs after calling the method new myTask.cancel(). I call the method when i press the back button, or when i change the fragment with a spinner. I have logged the progress of increments, and even after the myTask.cancel is called, the increments still rises.

My AsyncTask class

 public class myTask extends AsyncTask<Void, Integer, String> {
        private static final String TAG = "myTask";
        private ProgressBar budgetProgressBar=null;
        private TextView textView1;
        double b_val,e_val;
        int position;
        private Context mContext;

        public myTask(){
        }


        public myTask(double b_val ,double e_val,ProgressBar budgetProgressBar, TextView textView1) {
            this.b_val = b_val;
            this.textView1=textView1;
            this.e_val = e_val;
            pStatus = (int) e_val;
            this.budgetProgressBar= budgetProgressBar;
            position = (int) budgetProgressBar.getTag();
            maxBudget = (int) this.b_val;
        }


        int pStatus;
        int maxBudget;


        @Override
        protected void onPreExecute() {
            budgetProgressBar.setMax(maxBudget);
            budgetProgressBar.setVisibility(View.VISIBLE);

               budgetProgressBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
    //        }
        }

        @Override
        protected String doInBackground(Void... params) {

            int incrementor =10;


                for (int i = incrementor; i<=pStatus && !isCancelled(); i+=incrementor){

                    if (isCancelled()){
                        Log.d(TAG, "doInBackground: CANCELLED");
                        break;
                    }

                    try {

                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    publishProgress(i);
                }


            return "Complete";
        }


        @Override
        protected void onProgressUpdate(Integer... p) {

            super.onProgressUpdate(p);
            Log.d(TAG, "onProgressUpdate: ");
            if ((int) budgetProgressBar.getTag() == position){
                budgetProgressBar.incrementProgressBy(p[0]);
                budgetProgressBar.setProgress(p[0]);
                textView1.setText(p[0]+"/"+maxBudget);
               // Log.d(TAG, "Setting progress to " + p[0]);

                double calc =(e_val/b_val)*100;
                if (calc>100){
                    calc-=100;
                    textView1.setText("-"+ ((int) calc)+"%");
                }else{
                    textView1.setText(((int) calc)+"%");
                }

            }

        }





    }

Cancelling the AsyncTask

new myTask().cancel(true);
fritz-playmaker
  • 693
  • 1
  • 10
  • 15

2 Answers2

4

The AsyncTask#cancel does not stop the doInBackground task even if the method name suggests it as that. What happens is that doInBackground will run until the end and it will call onCancel instead of onPostExecute in the end.

So you might want to add a volatile field flag to AsyncTask and set that as cancelled for example

AsyncTask
private volatile boolean isRunning = true;

public void cancelTask() // sets isRunning to false

And you would check for isRunning instead of isCancelled().

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
1

please read this document for better understand of AsyncTask in Android https://developer.android.com/reference/android/os/AsyncTask.html

Nimisha V
  • 461
  • 4
  • 12