Reputation: 23521
If the user taps the Home key, I can pause my application because the onPause
event fires.
However, if the user long-presses Home, the recent app launcher pops up and my application carries on running behind it.
I want to pause my application in this situation. What can I do?
(NB. I don't want to prevent the long-press of Home or alter its behaviour in any way. My app just needs to know it's happened. It's a game, so I want to pause it.)
Upvotes: 1
Views: 672
Reputation: 11
You can also try to override onWindowFocusChanged in activity. Something like:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d(TAG, "On windows focus changed. (" + hasFocus + ")");
if (!hasFocus) doStopGame();
}
However, It's still not clear to me how to handle event when you are trying to get back to your app from the recent apps dialog. The above handler function is not called in this case. However, you can still pause you game and provide gamer ) with the dialog to resume it.
Upvotes: 1
Reputation: 10738
If all else fails, give the user the ability to manually pause (via button or menu).
It sounds like you've got a situation where the user would be unhappy with your game continuing during the long-press, but at this point the user has already shifted focus away from the game. If I were in the midst of a time-sensitive game and wanted to navigate away by long-pressing the Home button, my first thought would be to manually pause the game.
Just food for thought, good luck.
Upvotes: 1
Reputation: 5497
You could override the onKeyLongPress() method in your activity, do what you want, and then just call the super method.
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Upvotes: -1