0

I have tried the following ways to Override the HOME Button:-

  1. By using KeyDown method.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("Pressed", "" + keyCode);
    
    if (keyCode == KeyEvent.KEYCODE_HOME)
    {
        Log.e("HOME_PRESSED", "Ignoring HOME Button.");
        return true;
    }       
    else if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        Log.e("BACK_PRESSED", "Ignore back pressing.");
        return true;
    }
            return super.onKeyDown(keyCode, event);
    }
    
  2. By using NewIntent method.

    @Override
        protected void onNewIntent(Intent intent) {     
    super.onNewIntent(intent);      
    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_MAIN))
    {
        Log.e("MAIN_PRESSED", "Ignoring MAIN Button.");
    }
    }
    

But I am unable to Override the HOME Button Press Event.

So, as per my knowledge and by Logs I figured out that everytime I press Home button.

It fires ACT-AM_ON_PAUSE_CALLED. (Unaware about it)

I tried to Google this thing to know more about it but nothing relevant found!.

Please help to Override the HOME Button in application.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
  • Take a look this link: http://stackoverflow.com/questions/5039500/android-overriding-home-key – Gil Moshayof Oct 09 '13 at 12:19
  • 2
    Why? Why should your app behave differently to all the other apps and in a way the user does not understand? Apart from a launcher, if an app found a way to override home (which would be exploiting a bug) then it would not stay on my device for very long. – Simon Oct 09 '13 at 12:21
  • 1
    @Simon I think it's just another stupid requirement by his boss, I myself got used to be asked stupid and strange things! – Ahmad Dwaik 'Warlock' Oct 09 '13 at 12:26
  • 1
    this is not possible yet,so don't waste your time on it!!! – dd619 Oct 09 '13 at 12:45
  • Thanks for your response guys! Actually it's an Alarm Activity which is also running alarm tone & it's event has style=TRANSPARENT. So if user clicks HOME Button. It vanishes but alarm runs and even don't shows in Recent Apps, so need to override it to atleast destroy the alarm. – Harpreet Oct 09 '13 at 12:49
  • You need to learn about the Activity life cycle. When the user presses Home, `onPause()` is called. You can do whatever you need to in there. – Simon Oct 09 '13 at 15:54

3 Answers3

2

It's generally not recommended to override the Home button of the Android OS. The home button is a "safety" button when things go wrong in an application and you just want to get out of there. It is sort of an "eject" button if you will.

You can Override it to some degree by declaring it in the manifest as an application/action that the user can chose whenever the Home button is hit and set your app as the default option whenever the Home button is pressed. However, this method would ask the user to choose between your app and the default home action and assign a default (otherwise he would have to chose between those two options every time he presses the home button) - which isn't recommended at all (bad user experience).

Razgriz
  • 7,179
  • 17
  • 78
  • 150
1

From accepted answer here you cant override it but you can add some code to onStop() which is called upon home button click

Community
  • 1
  • 1
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
1

That is impossible and it has been prohibited to Override it (security issues). This is handled by the framework and you can't do what you like with it.

g00dy
  • 6,752
  • 2
  • 30
  • 43