title: How to stop Asynctask
body: <p>I want  to cancel Asynctask when i click the button(cancelsync) but the onProgressUpdate() method is executed and it Shows the Error: Activity has leaked window as it tries to execute alert.show() after the activity is changed. So what i can do to stop this AsyncTask
  after the cancelsync is clicked</p>
  
  <pre><code>CancelButton:
  
   cancelsync.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                      alert.setTitle(R.string.set4Play);
                      alert.setMessage(getString(R.string.interrupted));
                      alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                      alert.setButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if(communicator != null &amp;&amp; !communicator.isCancelled())
                                communicator.cancel(true);
                                Intent myintent = new Intent(NeliteMusicActivity.this,HomeScreen.class);
                                startActivity(myintent);
                              return;
                          } 
                          }); 
  
                      alert.setCanceledOnTouchOutside(false);
                      alert.show();
                      isTrue = false;
                  }
              });
  
  AsyncTask:
  
  
         class Communicator extends AsyncTask&lt;String, String, String&gt; 
          {
          @Override
                  protected void onProgressUpdate(String... values) {
                      super.onProgressUpdate(values);
                      int a=messag;
                      progress.setProgress(a);
                      textsync.setText("Syncing mix "+c+"/"+list.size());
                      if(progress.getMax()&lt;=a)
                      {
                                  if (isOnline()) 
                                  {
                                      final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                                      alert.setTitle(R.string.set4Play);
                                      alert.setMessage(getString(R.string.updatedMix));
                                      alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                                      alert.setButton(getString(R.string.ok), 
                                              new DialogInterface.OnClickListener() 
                                      {
                                            public void onClick(DialogInterface dialog, int which) {
                                                alert.dismiss();
                                                animatedStartActivity();
                                          }
                                          });
                                      alert.setCanceledOnTouchOutside(false);
                                      alert.show();
                                  }
                              if (!isOnline()) {
                                  final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                                  alert.setTitle(R.string.set4Play);
                                  alert.setMessage(getString(R.string.connectionerror));
                                  alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                                  alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            alert.dismiss();
                                            animatedStartActivity();
                                      } 
                                      }); 
                                  alert.setCanceledOnTouchOutside(false);
                                  alert.show();
                              }
  
                      }
          }
  </code></pre>