How do I determine if the Home key is presse开发者_Python百科d? If it's pressed I want to do some handling and finish the activity since I don't want it to resume next time it's started. I can't handle it in onStop
, since from the activity I start another activity(so onStop
will get called even in this case).
How do I determine if the Home key is pressed?
You don't.
If it's pressed I want to do some handling and finish the activity since I don't want it to resume next time it's started. I can't handle it in onStop, since from the activity I start another activity(so onStop will get called even in this case).
There are many ways in which a user can leave one of your activities, including:
- By you starting another activity
- By the user pressing HOME
- By the user pressing BACK
- By the user receiving a phone call, or some other activity popping up out of nowhere (e.g., alarm clock)
- By the user responding to a notification
- By the user rotating the device, putting it in a dock, or otherwise triggering a configuration change
Generally speaking, you have no idea which of those has occurred. isFinishing()
will cover #3, and there are a couple of ways to identify #6, but the others are indistinguishable from a system standpoint.
If you wish to treat scenario #1 as special -- doing something different because the user is moving to one of your activities rather than something else -- then that's up to you to implement.
精彩评论