-1

In android i have been able to override the funcationality of back button very easily but for my app i need to override the home button. I am sure it can be done but how can i achieve this.

Harshit Rathi
  • 2,273
  • 2
  • 17
  • 26
  • NO! you can not override android home key click event it prevented by system, only onStop method will call on home key click event. – Vijju Mar 10 '14 at 11:17
  • but when press home button i want to show an alert so how can i do this. – Harshit Rathi Mar 10 '14 at 11:19
  • on click of home key only onStop of activity will call, therefor displaying an alert on stop is not possible. – Vijju Mar 10 '14 at 11:23

2 Answers2

0
@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

and then

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {
        finish();
   }
 return false;
} 
Sri
  • 718
  • 1
  • 6
  • 21
0

Sorry to say but, It is not possible.Refer the link:-

http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME`

Key code constant: Home key. This key is handled by the framework and is never delivered to applications.

Many blogs suggest to use onPause() method of activity life cycle,for the same.But it takes a lot to manage.

I will suggest to use onUserLeaveHint() method.

Harshal Benake
  • 2,429
  • 1
  • 23
  • 41