-3

Is it possible to cancel a countdown timer in the onResume() method ?

For example: when the back button is pressed the onResume() method is called. I want to cancel the timer when it is in the login activity, in case of any other activity I want the timer to continue running.

I have thought about writing an if() condition in the onResume() activity and cancelling the timer but the timer does not stop.

My approach could be wrong, so please guide me.

void onResume(){
    super.onResume();
    if(currentActivity.equals("LoginActivity")){
        timer.cancel;
    }
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
user1420943
  • 97
  • 1
  • 1
  • 7
  • Take a look at [this](http://stackoverflow.com/questions/2441145/onpause-onresume-activity-issues) question. It's not the exact same, but is similar. – thegrinner Jul 11 '12 at 12:06

2 Answers2

1

You can stop the countdown timer whenever the back button is pressed in the login activity. For that use the following code in your login activity.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //stop the timer
        }
        return super.onKeyDown(keyCode, event);
    }
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • public boolean onKeyDown(int keyCode, KeyEvent event) { Log.v (TAG,"keydown"); if (keyCode == KeyEvent.KEYCODE_BACK) { timer.cancel(); } return super.onKeyDown(keyCode, event); } does not work :(..am i going wrong some whre – user1420943 Jul 11 '12 at 13:52
0

If you use TimerTask then cancel TimerTask.

mTimerTask.cancel();

else cancel timer.

mTimer.cancel();
mTimer.purge(); 

I think this code may help you.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mehul Santoki
  • 1,208
  • 1
  • 12
  • 25