Call method when home button pressed
Asked Answered
F

11

53

I have this method in one of my Android Activities:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Log.d("Test", "Back button pressed!");
    }
    else if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        Log.d("Test", "Home button pressed!");
    }
    return super.onKeyDown(keyCode, event);
}

But, even though the KEYCODE_HOME is valid, the log method never fires. This works for the back button though. Does anyone know why this is and how to get this to work?

Feme answered 24/1, 2011 at 15:51 Comment(3)
https://mcmap.net/q/82966/-detect-home-button-press-in-androidStacistacia
@Yvette I agree. However only for the reason. It should probably be closed anyway, as on reflection it's not a great thing to try and detect - and therefore not great for future visitors to SO. There are better ways to stop services (which is what my real question was here), and you cannot override the home button as some of the answers talk about below.Feme
Use onTrimMemory() as suggest from hereSybilla
H
35

The Home button is a very dangerous button to override and, because of that, Android will not let you override its behavior the same way you do the BACK button.

Take a look at this discussion.

You will notice that the home button seems to be implemented as a intent invocation, so you'll end up having to add an intent category to your activity. Then, any time the user hits home, your app will show up as an option. You should consider what it is you are looking to accomplish with the home button. If its not to replace the default home screen of the device, I would be wary of overloading the HOME button, but it is possible (per discussion in above thread.)

Hellgrammite answered 24/1, 2011 at 15:59 Comment(3)
What about if I want to stop the media player? If I press Home it continues to play...Feme
I dont need to override the home button, I just need to stop the music before the app disappears!Feme
Just detect if your activity goes to background.Sushi
A
22

It took me almost a month to get through this. Just now I solved this issue. In your activity's onPause() you have to include the following if condition:

    if (this.isFinishing()){
        //Insert your finishing code here
    }

The function isFinishing() returns a boolean. True if your App is actually closing, False if your app is still running but for example the screen turns off.

Hope it helps!

Akers answered 13/8, 2011 at 12:34 Comment(2)
The code should be if (!this.isFiishing()) { //home button pressed } (just add a "!")Poult
The iwind's comment is correct. More exact: isFinishing() will be false if HOME button or TABS button is pressed or the screen turns off. (Will be true if BACK button is pressed.)Phenology
D
13

The HOME button cannot be intercepted by applications. This is a by-design behavior in Android. The reason is to prevent malicious apps from gaining control over your phone (If the user cannot press back or home, he might never be able to exit the app). The Home button is considered the user's "safe zone" and will always launch the user's configured home app.

The only exception to the above is any app configured as home replacement. Which means it has the following declared in its AndroidManifest.xml for the relevant activity:

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.HOME" />
   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

When pressing the home button, the current home app's activity's onNewIntent will be called.

Distributee answered 24/1, 2011 at 16:5 Comment(2)
Doesn't work. Documentation on onNewIntent btw: "This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent.FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity. In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it"Jamboree
Worked for me when used with home replacement app (i.e. custom launcher) I am developing.Brambly
C
8

I found that when I press the button HOME the onStop() method is called.You can use the following piece of code to monitor it:

@Override
    protected void onStop() 
    {
        super.onStop();
        Log.d(tag, "MYonStop is called");
        // insert here your instructions
    }
Cetology answered 25/3, 2012 at 20:55 Comment(7)
It works, but it will be called when you go to another Activity too. So, it's not called only when you are exiting the app.Stacistacia
To be honest, this answer is just wrong. onStop is called on various occasitions (as italo stated), not only when the Home button is being pressed !Untold
As i was reading the thread had decided to answer with the onStop() override. this is the only was it'll work, without adding all the category and other stuff.Staggers
Notice that onStop will usually be called even on screen rotationBlood
@italo Yes, that's my exact problem. Do you know what method I should use to just have it execute whenever the user leaves my app?Virgo
Wrong answer. Activity's onStop will be called for several reasons, not just for home button pressNodarse
Wrong answer , onStop is activity life-cycle method and not related to home buttonCork
B
3

KeyEvent.KEYCODE_HOME can NOT be intercepted.

It would be quite bad if it would be possible.

(Edit): I just see Nicks answer, which is perfectly complete ;)

Bijouterie answered 24/1, 2011 at 16:0 Comment(0)
A
3

I have a simple solution on handling home button press. Here is my code, it can be useful:

public class LifeCycleActivity extends Activity {


boolean activitySwitchFlag = false;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        activitySwitchFlag = true;
        // activity switch stuff..
        return true;
    }
    return false;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override 
public void onPause(){
    super.onPause();
    Log.v("TAG", "onPause" );
    if(activitySwitchFlag)
        Log.v("TAG", "activity switch");
    else
        Log.v("TAG", "home button");
    activitySwitchFlag = false;
}

public void gotoNext(View view){
    activitySwitchFlag = true;
    startActivity(new Intent(LifeCycleActivity.this, NextActivity.class));
}

}

As a summary, put a boolean in the activity, when activity switch occurs(startactivity event), set the variable and in onpause event check this variable..

Avertin answered 16/9, 2011 at 14:0 Comment(1)
That's not a bad idea. While it doesn't actually tell you if the home button is pressed you can assume. I guess this method calls when the phone starts ringing?Feme
A
3

Using BroadcastReceiver

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // something
    // for home listen
    InnerRecevier innerReceiver = new InnerRecevier();
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    registerReceiver(innerReceiver, intentFilter);

}



// for home listen
class InnerRecevier extends BroadcastReceiver {

    final String SYSTEM_DIALOG_REASON_KEY = "reason";
    final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            if (reason != null) {
                if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                    // home is Pressed
                }
            }
        }
    }
}
Aurita answered 22/8, 2018 at 3:48 Comment(0)
K
2

use onPause() method to do what you want to do on home button.

Kalinin answered 8/10, 2011 at 19:19 Comment(1)
onPause is alos launched when the screen switches off... onStop is better.Sanskrit
M
1

I also struggled with HOME button for awhile. I wanted to stop/skip a background service (which polls location) when user clicks HOME button.

here is what I implemented as "hack-like" solution;

keep the state of the app on SharedPreferences using boolean value

on each activity

onResume() -> set appactive=true

onPause() -> set appactive=false

and the background service checks the appstate in each loop, skips the action

IF appactive=false

it works well for me, at least not draining the battery anymore, hope this helps....

Mccartney answered 1/6, 2011 at 8:1 Comment(1)
Again, this is only true if your application has a single activity. If you switch to another activity in the same app, you will have set the appActive variable to true.Culminant
K
0

define the variables in your activity like this:

const val SYSTEM_DIALOG_REASON_KEY = "reason"
const val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
const val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"

define your broadcast receiver class like this:

class ServiceActionsReceiver: BroadcastReceiver(){
        override fun onReceive(context: Context?, intent: Intent?) {
            
            val action = intent!!.action
            if (action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
                val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY)
                if (reason != null) {
                    if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) {
                        //do what you want to do when home pressed
                    } else if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) {
                        //do what you want to do when recent apps pressed
                    }
                }
            }
        }
    }

register reciver on onCreate method or onResume method like this:

val filter = IntentFilter()
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
registerReceiver(receiver, filter)

add receiver in your manifest like this:

<receiver android:name=".ServiceActionsReceiver">
            <intent-filter >
                <actionandroid:name="android.intent.action.CLOSE_SYSTEM_DIALOGS"/>
            </intent-filter>
</receiver>
Kuvasz answered 28/4, 2021 at 8:48 Comment(0)
U
0

https://www.tutorialspoint.com/detect-home-button-press-in-android

  @Override
   protected void onUserLeaveHint() {
      text.setText("Home buton pressed");
      Toast.makeText(MainActivity.this,"Home buton pressed",Toast.LENGTH_LONG).show();
      super.onUserLeaveHint();
   }
Undersexed answered 13/9, 2022 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.