开发者

Android : How to determing home key is pressed

开发者 https://www.devze.com 2023-02-27 11:23 出处:网络
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 h

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:

  1. By you starting another activity
  2. By the user pressing HOME
  3. By the user pressing BACK
  4. By the user receiving a phone call, or some other activity popping up out of nowhere (e.g., alarm clock)
  5. By the user responding to a notification
  6. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消