Problems To Cancel A CountDownTimer Android Java

KD-21 picture KD-21 · May 13, 2014 · Viewed 14.8k times · Source

When I close my app by pressing the BACK button (onBackPressed() called), the CountDownTimer doesn't stop, until it is done with counting. How can I put the CountDownTimer cancel(); in my onBackPressed()?

Because, when I quit my application (shown below with descriptions) I don't want counter toasts on my screen anymore.

On top of my code:

boolean network_connected = false;

What's in my onCreate():

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;
}   

onBackPressed() method

@Override
public void onBackPressed() {
    if (page_number > 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
    }
}

Thanks.

Answer

Apoorv picture Apoorv · May 13, 2014

Create a global object of CountDownTimer eg.

On top of the main_activity class set: CountDownTimer timer; after that do the things below.

timer = 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
}

and on onBackPressed call timer.cancel(); like

@Override
public void onBackPressed() 
{
  if (page_number > 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
  }

 timer.cancel();
}