0

How do I determine if the Home key is pressed? 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).

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Android_enthusiast
  • 863
  • 5
  • 22
  • 41
  • As far as I know (now) : you cannot determine that. And question may be duplicated of http://stackoverflow.com/questions/1998278/android-capture-suppress-home-and-endcall-buttons-events – OcuS Apr 18 '11 at 22:33
  • No, its not a duplicate of that question... cna anyone please answer this,. – Android_enthusiast Apr 18 '11 at 22:37
  • possible duplicate of [How can I detect user pressing HOME key in my activity?](http://stackoverflow.com/questions/2208912/how-can-i-detect-user-pressing-home-key-in-my-activity) – cHao Sep 20 '11 at 17:25

1 Answers1

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So, can I set some flag if I am launching another activity with in an activity. If flag is set don't do anything if its not set then in onStop - do the processing and finish the application(Assuming its ending because of HOME key)?? Also, I checked the behavior in messaging app... if user in middle of composing masg and home key is pressed they save to drafts, how can the app know in this case.. plz clarify.. – Android_enthusiast Apr 18 '11 at 22:54
  • @Android_enthusiast: Ideally, you redesign your application such that you do not care how a user leaves your activity. In terms of the Messenger application, you are welcome to browse the source code for it over on http://source.android.com. – CommonsWare Apr 18 '11 at 23:07