title: How to stop ( kill ) AsyncTask in android
body: <p>Hi I am new to android: </p>
  
  <p>I am displaying image thumbs in GridView. For better performance I am loading it asynchronously.</p>
  
  <p>My AsyncTask is as:</p>
  
  <pre><code>class BitmapWorkerTask extends AsyncTask&lt;Integer, Void, Bitmap&gt; {
          private final WeakReference&lt;ImageView&gt; imageViewReference;
          private int data = 0;
          private String image_path;
  
          public BitmapWorkerTask(ImageView imageView) {
              imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
          }
  
          @Override
          protected Bitmap doInBackground(Integer... params) {
              data = params[0];
              Bitmap picture = BitmapFactory.decodeFile(image_path);
              int width = picture.getWidth();
              int height = picture.getHeight();
              float aspectRatio = (float) width / (float) height;
              int newWidth = 98;
              int newHeight = (int) (98 / aspectRatio);
              return picture = Bitmap.createScaledBitmap(picture, newWidth,
                      newHeight, true);
  
          }
  
          @Override
          protected void onPostExecute(Bitmap bitmap) {
              if (imageViewReference != null &amp;&amp; bitmap != null) {
                  final ImageView imageView = imageViewReference.get();
                  if (imageView != null) {
                      imageView.setImageBitmap(bitmap);
                  }
  
              }
          }
  
          public void onDismiss(DialogInterface dialog) {
              this.cancel(true);
          }
      }
  </code></pre>
  
  <p>and Calling form:</p>
  
  <pre><code>public View getView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater layoutInflater = (LayoutInflater) ctx
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          ListRowHolder listRowHolder;
          if (convertView == null) {
              convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                      parent, false);
              listRowHolder = new ListRowHolder();
              listRowHolder.imgSponsor = (ImageView) convertView
                      .findViewById(R.id.imggrid_item_image);
              convertView.setTag(listRowHolder);
  
          } else {
              listRowHolder = (ListRowHolder) convertView.getTag();
          }
          try {
              BitmapWorkerTask task = new BitmapWorkerTask(
                      listRowHolder.imgSponsor);
              task.image_path = ImageName.get(position);
              task.execute(1);
  
          } catch (Exception e) {
              Toast.makeText(ctx, e + "", Toast.LENGTH_SHORT).show();
          }
  
          return convertView;
      }
  </code></pre>
  
  <p>The  problem here is the tasks are running in background even I clicked on back button.</p>