title: Asynctask doesn't stop after cancelling it in doInBackground
body: <p>I'm trying to code a progress bar, that increases depending on a timer (starts at 0 and goes up to a certain time I choose).
  When I click on a button, the progress bar decreases (decreases from values[0] - 5).
  When the progress bar value is 30, I want to stop the progressbar, so stop asynctask, and restart it so the progress bar value is now 0.</p>
  
  <p>My code doesn't have any error, the problem is that the progressbar doesn't stop and doesn't restart. (I know that because I made a TOAST to print the value of the progress bar, and it never stops)</p>
  
  <p>This is my code in MainActivity</p>
  
  <p>public class MainActivity extends Activity {</p>
  
  <pre><code>ProgressBar progressBar_eau;
  
  TextView txt;
  int compteur = 0;
  
  Button btn_arrosoir; 
  
  MyTask task_eau;
  Boolean restart = true;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      btn_arrosoir = (Button) findViewById(R.id.btn_arrosoir);//my button when you click the progress bar decreases
      progressBar_eau = (ProgressBar) findViewById(R.id.barre_eau);
      progressBar_eau.setMax(30);
      Drawable draw3=getResources().getDrawable(R.drawable.custom_eau);
      progressBar_eau.setProgressDrawable(draw3);
  
      progressBar_eau.setProgress(0);
  
      task_eau = new MyTask(progressBar_eau);
      task_eau.execute(300);
  
      btn_arrosoir.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //OnCLick Stuff
              compteur = compteur - 5;
  
          }
      });
  
  }
  
  
  public void cancelAsynctask(){
      if (restart == false){
          task_eau.cancel(true);
          restartAsynctask();
          Toast.makeText(MainActivity.this,
                          "cancel", Toast.LENGTH_LONG).show();
          restart = true;
      }
  
  }
  
  public void restartAsynctask(){
      task_eau = new MyTask(progressBar_lumiere);
              task_eau.execute(300);
              Toast.makeText(MainActivity.this,
                      "restart", Toast.LENGTH_LONG).show();//my emulator shows both message start and restart even though the progress bar doesn't stop
  }
  
  public class MyTask extends AsyncTask&lt;Integer, Integer, String&gt; {
  
      ProgressBar progressBar_actuelle;
      public MyTask(ProgressBar target) {
          progressBar_actuelle = target;
  
      }
  
  
       @Override
              protected String doInBackground(Integer... params) {
  
  
              while (task_eau.isCancelled() == false){
                  for (; count &lt;= params[0]; count++) {
  
                      try {
                       Thread.sleep(1000);
                                                  publishProgress(count);
  
                                              } catch (InterruptedException e) {
                                                  e.printStackTrace();
                                              }
                  }
                  }
  
                  return null;
              }
  
      @Override
      protected void onPostExecute(String result) {
  
      }
  
  
  
      @Override
      protected void onProgressUpdate(Integer... values) {
  
          progressBar_actuelle.setProgress(values[0]+compteur);
  
          if (progressBar_actuelle.getProgress() &gt;= 30){
              restart = false;
              cancelAsynctask();
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>