2

I am trying to catch when the user has pressed the Home button. I thought I could use

protected void onResume()
{
   registerReceiver(homeReceiver, new IntentFilter(Intent.CATEGORY_HOME));
}
...
private BroadcastReceiver homeReceiver = new BroadcastReceiver()
       {
          public void onReceive(Context context, Intent intent)
          {
             // logic here
          }
       };

but that doesn't seem to work. I understand that onPause will be called, but my particular app has some logic that I need to handle separately. Short story is based on some info coming in, the app will display new Activities to the user. To prevent Back button issues, as each Activity hits it's onStop it calls finish on itself. However if the user presses the Home button I do not want the Activity to call finish so that when the user presses the app icon on the home screen or via the Recently run apps list, the last Activity is brought back.

I suspect that I have made this more complicated than planned. Any suggestions are appreciated.

bursk
  • 1,647
  • 7
  • 25
  • 39
  • I'm sorry what are you trying to accomplish here? By default you have the behavior you seem to want. No need to hijack the back or home buttons. – Nathan Schwermann Nov 04 '10 at 21:24
  • The longer story is Activity A kicks off B, which, depending on the incoming data will display C, D, or F. The display of C, D, or F can be changed as new data comes in. I want the Back button to be the exit button. If C, D, and F do not call finish in their onStop then pressing the Back cycles back through each previous display. Because I am currently not able to tell when the Home button is pressed, the current Activity displayed hits its onStop and calls finish. But then when the user presses the icon to go back the app/Android doesn't go back to where we left off. – bursk Nov 04 '10 at 21:54
  • have you tried using startActivityForResult ? – Nathan Schwermann Nov 04 '10 at 22:32
  • I have, if you have any ideas how to use that to my advantage I'd welcome them. thanks – bursk Nov 05 '10 at 02:05

6 Answers6

3

You can monitor the life cycle of activity. When the home button is pressed following sequence of methods is called:

onPause()
onStop()
onRestart()
onStart()
onResume()

When you start a new actvity:

onPause()
onStart()
onResume()
onStop()

When you back from some activity already started:

onPause()
onRestart()
onStart()
onResume()
onStop()
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
3

I'm not good in English (from Russia). Yes, u can't catch the HOME-button click. Only way I found is to read log (logcat) and parse results. When HOME activity is running - the next record appears in logcat:

Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/.Launcher }

U can parse this entry by regular expression. So, this is the best way I found to catch click on HOME

UAS
  • 405
  • 2
  • 4
  • 9
1

The home button can not be overriden. You can only catch the intent and display a diferent home, but the user will always be able to choose.

Dont mess with how hard buttons work so much, user like consistent behaviour.

blindstuff
  • 18,298
  • 10
  • 47
  • 48
  • 3
    I'm not trying to override the Home button behavior, I just want to know it has been pressed so I can know to not call onFinish – bursk Nov 05 '10 at 02:03
0

An easiest way is to put a boolean variable at false when you exit from that activity to go in some other activity. Then put the variable at true in the onResume method. Check in the method onStop() if it is not false, so if it is true then the Home button is pressed.

Something like:

boolean exit=false;

...

//wherever other activity starting
exit=false;
startActivity(activity);

...

@Override
protected void onResume() {
    super.onResume();
    exit=true;
}

...

@Override
protected void onStop() {
    super.onStop();
    if(exit){
        //Home button is pressed
    }
}
Mok
  • 35
  • 4
0

button behavior, I just want to know it has been pressed so I can know to not call onFinishnot call onFinish – cbursk

You can overide onPause() right? onFinish() not called if home button pressed... onFinish() is called when back button is pressed... and back button can be override with onKeyDown(), like this

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // action goes here
    } else {
        return super.onKeyDown(keyCode, event);
    }
}
Tek Yin
  • 3,011
  • 3
  • 25
  • 42
  • I have that code implemented to catch the back and call finish on the Activity. The trick is that I've got these Activities C, D, and F. They may be navigated to and from by the user. So lets say first B displays C, then the user goes to F, then D, F, C, D, C. Then when the user presses the Back button, I catch it and call finish, in the example above C goes away and D is displayed, hit back, C, and so on. – bursk Nov 05 '10 at 09:41
  • So I've basically starting digging this hole to avoid back button hell – bursk Nov 05 '10 at 10:04
  • if you keep opening an `Activities`, it will stack like card. So when you hit `back` button on top activity, it will back 1 step to activity opened before the last.... Just like in your case, when you in C, click button, and it will show D. Back again, C showed... If you don't want to stack like that, just put `finish()` when launch an activity. – Tek Yin Nov 08 '10 at 07:44
0

Long story short is that I gave up on trying to catch that the Home button is being pressed. I'm assuming that it is the finish() that each Activity was calling on itself in their onStop that is the issue. Since I still need to call finish() on each Activity to avoid back button issues I updated B to first kick off the new Activity with startActivityForResult and then immediately call finishActivity on the previous Activity. This appears to have solved the overall issue. Now when I press the Home button and minimize the app, when I select the app's icon from the Home screen or Home's recently run apps list I get the correct Activity brought to the foreground.

Now I just need to figure out why - sometimes - pressing the Home button is causing my Services to stop. But that will be a new question if I can't get it figured out. Thank you everyone for your ideas.

bursk
  • 1,647
  • 7
  • 25
  • 39
  • Just and FYI, I discovered the reason my Services were stopping was because I was using bindService to start them instead of startService. – bursk Nov 08 '10 at 17:39
  • 1
    I still think a lot of this would be easier if you could just catch the Home button click. – bursk Nov 08 '10 at 17:39