Reputation: 1042
In my game I have a countdown timer, and when I press home button and leave the game, the timer goes on and when it hits 0 my game pops back on, cause the time is up, and I get some info screen for time up. How to kill the timer at least when I hit the home button? I tried with this but it didn't work:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
timer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 0
Views: 230
Reputation: 9103
Override Activity onUserLeaveHint()
Method description from javadoc:
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key
Above solution works for HOME button just fine. However, consider overriding onPause()
method of your Activity
and cancel timer there. This way you will cancel timer whenever your game is paused. Unless you treat HOME as a special case, then follow my first part of the answer.
Upvotes: 1