title: Unable to cancel Async task on button click
body: <p>I am trying to cancel/end an async task on the button click of a pop-up box. However, when I click on the button the onCancelled() method is not being called. Could someone please help me with this? Below is my code:</p>
  
  <pre><code>public AuthenticationAsync(Context context, final String rid) {
      this.rid = rid;
  
      alertDialog = new AlertDialog.Builder(context).create();
      alertDialog.setTitle("Information");
      alertDialog.setMessage("RaspberryPi Validated");
      alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
              new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                      cancel(true); //Cancelling async task on button click
                      dialog.dismiss();
                  }
              });
  }
  
  
  @Override
  protected Object doInBackground(Object[] params) {
      String metricData = String.valueOf(kuraPayload.getMetric("data"));
      if (metricData.isEmpty()) 
          subResponse = false;
  }
  
  @Override
  protected void onCancelled() {
      Log.d("Async","cancelled");  //not being called
  }
  
  @Override
  protected void onPostExecute(Object o) {
      if (subResponse) {
          asyncHttpClient = new AsyncHttpClient();
          asyncHttpClient.get(WSConstants.ADD_RPI_WS + MQTTFactory.getRaspberryPiById(), new AsyncHttpResponseHandler() {
  
              @Override
              public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                  Toast.makeText(ActivityContexts.getMainActivityContext(), "RaspberryPi Validated", Toast.LENGTH_LONG).show();
                  alertDialog.show();
              }
  
      } 
  }
  }
  </code></pre>