title: How to check if context can still be used in android?
body: <p>In my android app I have a async task, that downloads an image and saves it to external storage, then repeats until all images are downloaded. This can take a while, and I reference the context from the activity that called it, a couple times in the async per downloaded image.</p>
  
  <p>During this process the user could have pressed back or something which ends the context.</p>
  
  <p>How can I check if I can still use it in the async task?</p>
  
  <p>This is my async task code:</p>
  
  <pre><code>package async;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.util.List;
  import common.Common;
  import common.FishCategory;
  import http.Network;
  import interfaces.ImageDownloader;
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.os.AsyncTask;
  import android.os.Environment;
  import android.provider.MediaStore;
  
  public class Async_getAllFishPics extends AsyncTask&lt;FishCategory, Void, FishCategory&gt; {
  
      String link;
      ImageDownloader callerContext;
      List&lt;FishCategory&gt; categoryList;
  
      public Async_getAllFishPics(ImageDownloader callerContext, List&lt;FishCategory&gt; categoryList) {
          this.callerContext = callerContext;
          this.categoryList = categoryList;
  
          for (FishCategory fish : categoryList) {
              link = fish.getLink();
              if (!link.equals("")) {
                  String imgpath = Common.getImagePath(link);
                  File file = new File(android.os.Environment.getExternalStorageDirectory(), imgpath);
                  if (!file.exists() &amp;&amp; Network.isNetworkAvailable((Context)callerContext)) {
                      execute(fish);
                      break;
                  }
              }
          }
      }
  
      @Override
      protected FishCategory doInBackground(FishCategory... fishes) {
          Bitmap bitmap = Network.DownloadImage((Context) callerContext, link);
  
          try {
              saveImage(bitmap);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          return fishes[0];
      }
  
      @Override
      protected void onPostExecute(FishCategory fish) {
          try {
              fish.setLink(link);
              new Async_getAllFishPics(callerContext, categoryList);
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      private void saveImage(Bitmap bitmap) throws IOException {
          if (bitmap != null) {
              String path = Environment.getExternalStorageDirectory().toString();
  
              String imgpath = Common.getImagePath(link);
  
              File file = new File(path, imgpath);
              file.getParentFile().mkdirs();
  
              String abs = file.getAbsolutePath();
  
              OutputStream fOut = new FileOutputStream(file);
              bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  
              fOut.flush();
              fOut.close();
  
              MediaStore.Images.Media.insertImage(((Activity)callerContext).getContentResolver(),abs,file.getName(),file.getName());
          }   
      }
  }
  </code></pre>