title: Problems To Cancel A CountDownTimer Android Java
body: <p>When I close my app by pressing the BACK button (onBackPressed() called), the <code>CountDownTimer</code> doesn't stop, until it is done with counting. How can I put the <code>CountDownTimer</code> <strong>cancel();</strong> in my <code>onBackPressed()</code>? </p>
  
  <p>Because, when I quit my application (shown below with descriptions) I don't want counter toasts on my screen anymore.</p>
  
  <p><strong>On top of my code:</strong></p>
  
  <pre><code>boolean network_connected = false;
  </code></pre>
  
  <p><strong>What's in my <code>onCreate()</code>:</strong></p>
  
  <pre><code>if (check_network.isInternetAvailable(this)) {
      network_connected = true;
      new connect_task_main().execute("");
  } else {
      network_connected = false;
  }
  
  if (network_connected == false) {
      new CountDownTimer(11000, 1000) {
          public void onTick(long millisUntilFinished) {
              global.toast.setText("No Internet Connection!" + "\n" + "Automatic Refresh In: " + millisUntilFinished / 1000); //set text for toast
              global.toast.show(); //show toast
          }
  
          public void onFinish() {
              if (network_connected == false) {
                  global.cancel_toast(0); //stop all toasts
                  finish(); //quit activity
                  startActivity(new Intent(main_activity.this, main_activity.class)); //start activity
              } else {
              }
          }
      }.start(); //start the countdowntimer
  } else {
      network_connected = true;
  }   
  </code></pre>
  
  <p><strong><code>onBackPressed()</code></strong> method</p>
  
  <pre><code>@Override
  public void onBackPressed() {
      if (page_number &gt; global.page_number_min) { //does not matter
          page_number--; //does not matter
          global.cancel_toast(0); //stop all toasts
          network_connected = true;
          finish();
      } else {
          global.cancel_toast(0);
          network_connected = true;
          finish(); //quit activity
          super.onBackPressed(); //quit application
      }
  }
  </code></pre>
  
  <p>Thanks.</p>