title: How to kill a thread and handler before going to new activity
body: <p>Hey all - this code might be a bit messy in the way I'm trying to clean up the handler as I've been trying to track down where the crash happens...</p>
  
  <p>I've got a dialog activity that is showing a password entry with a progressbar being animated by a thread and handler...</p>
  
  <p>It seems that when I'm attempting to see if the progressbar is done, and trying to kill the thread, the way I'm doing this is messing something up when I try to go to a new activity - ie in a way calling a function and not having anything to return <em>to</em> or something...</p>
  
  <pre><code>public class RMO_Dialog extends Activity {
      private ProgressBar progbar;
      private Button dialogOK;
      private EditText dialogPass;
      private SharedPreferences prefs;
      private String pass;
      private int increment=10;
      private Thread background;
  
      private Boolean commCalled=false;
  
      public void callCommunications(){
          progbar.setVisibility(0);
          progbar.setProgress(0);
          background.stop();
          Toast.makeText(getApplicationContext(), "Call communication should happen once.", Toast.LENGTH_LONG).show();
  //      Intent i = new Intent();
  //      i.setClass(RMO_Dialog.this, RMO_Comm.class);
  //      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //      startActivity(i);
  //          finish();
      }
  
      public void buzzUser(){
  
          Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
          int dot = 200;
          int dash = 500;
          int short_gap = 200;
          int medium_gap = 500;
          int long_gap = 1000;
          long[] pattern = {0,dot, short_gap, dot, short_gap, dot, medium_gap, dash, short_gap, dash, short_gap, dash, medium_gap, dot, short_gap, 
                  dot, short_gap, dot, long_gap};
  
          v.vibrate(pattern, -1);
  
  
      }
  
      public void killCountdown(){
          progbar.setVisibility(0);
          progbar.setProgress(0);
          background.stop();
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.dialogpassword);
  
          buzzUser();
  
          prefs = this.getSharedPreferences("RMO", MODE_WORLD_READABLE);
          pass = prefs.getString("password", "");
  
          dialogOK = (Button) findViewById(R.id.dialogOK);
          dialogPass = (EditText) findViewById(R.id.dialogPass);
          progbar = (ProgressBar) findViewById(R.id.progress);
  
          progbar.setProgress(0);
  
          background = new Thread(new Runnable(){
              @Override
              public void run() {
                  try{
                      while(progbar.getProgress()&lt;=progbar.getMax()){
                          Thread.sleep(300);
                          progressHandler.sendMessage(progressHandler.obtainMessage());
                      }
                  }catch(java.lang.InterruptedException e){
                      Toast.makeText(getApplicationContext(), "Error thrown.", Toast.LENGTH_LONG).show();
                  }
  
              }
  
          });
          background.start();
  
          dialogOK.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(dialogPass.getText().toString().equals(pass.toString())){
                      killCountdown();
                      Toast.makeText(getApplicationContext(), "Guardian Angel next alert has been disengaged.", Toast.LENGTH_LONG).show();
                      Intent intent = new Intent();
                      intent.setClass(RMO_Dialog.this, RMO.class);
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      startActivity(intent);
                      finish();
                  }else{
                      callCommunications();
                  }
              }
          });
  
  
  
      }
  
      Handler progressHandler = new Handler(){
          public void handleMessage(Message msg){
              progbar.incrementProgressBy(increment);
              if(progbar.getProgress()==progbar.getMax()){
                  Toast.makeText(getApplicationContext(), "commcalled: "+ commCalled, Toast.LENGTH_LONG).show();
                  if(commCalled==false){
                      commCalled=true;
                      callCommunications();
                  }
  
              }
          }
      };
  }
  </code></pre>