bursk
bursk

Reputation: 1667

Android - How to catch that the Home button was pressed?

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.

Upvotes: 2

Views: 14124

Answers (6)

Mok
Mok

Reputation: 33

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
    }
}

Upvotes: 0

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()

Upvotes: 3

UAS
UAS

Reputation: 415

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

Upvotes: 3

bursk
bursk

Reputation: 1667

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.

Upvotes: 0

Tek Yin
Tek Yin

Reputation: 3050

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);
    }
}

Upvotes: 0

blindstuff
blindstuff

Reputation: 18348

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.

Upvotes: 1

Related Questions