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