title: What's a "nice" way of stopping a (linear) runnable in case of activity switch?
body: <p>I have a thread that will do some processing, then has runnable code which will display the results on the screen. The issue is that if the user presses the back arrow while the runnable is between two lines of display code, the next line will crash as the activity no longer exists.</p>
  
  <p>The below code accomplishes the goal, but I hate having <code>if</code> statements before each line. Is there a better way?</p>
  
  <pre><code>@Override
   public void onBackPressed() {
      super.onBackPressed();
      imgThread.interrupt();
  }
  
  private void processImage(){
          final Handler mHandler = new Handler(Looper.getMainLooper());
  
          progress = new ProgressDialog(this);
          progress.setMessage("TEXT");
          progress.setIndeterminate(true);
          progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          progress.show();
  
          imgThread = new Thread(){
              @Override
              public void run() {
  
                  //Process the image a bit
  
                  mHandler.post(new Runnable() {
                      @Override
                      public void run() {
                          if(!imgThread.isInterrupted())
                          imageView.setImageBitmap(mDisplayBitmap);
                          if(!imgThread.isInterrupted())
                          progress.setMessage("TEXT TEXT");
                          if(!imgThread.isInterrupted())
                          progress.show();
                      }
                  });
  
                  //More processing
  
                  mHandler.post(new Runnable() {
                      @Override
                      public void run() {
                          if(!imgThread.isInterrupted())
                          addScreenListener();
                          if(!imgThread.isInterrupted())
                          determineHelpToast("TEXT TEXT TEXT", Toast.LENGTH_LONG);
                          if(!imgThread.isInterrupted())
                          progress.dismiss();
                      }
                  });
  
              }
          }; imgThread.start();
      }
  </code></pre>