How to override hardware Home button in android? OR How to listen to home button click in android?

By | June 2, 2012

Hello everyone…

In this post I will show you how to listen to hardware button in an android smartphone.

This post especially shows how to listen to the home button in android. But my advice is never override the home button in your app.
Home button is to move the currently running process to background. But you can override the Backbutton and other buttons, android allows that on the keyDown Method. But if you try to match the “Home” button in the keyDown method it will not work. For that we have to follow another way which is shown in this post.

This simple java code handles the button pressed.

package test.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

public class Test2Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        	System.out.println("KEYCODE_HOME");
        	showDialog("'HOME'");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	System.out.println("KEYCODE_BACK");
        	showDialog("'BACK'");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_MENU)) {
        	System.out.println("KEYCODE_MENU");
        	showDialog("'MENU'");
            return true;
        }
        return false;
    }
    
    void showDialog(String the_key){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?")
    		  .setCancelable(true)
    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   dialog.cancel();
    	        	   finish();
    	           }
    	       })
    	       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.setTitle("CoderzHeaven.");
    	alert.show();
    }
    
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
    }
    
    public void onUserLeaveHint() { // this only executes when Home is selected.
    	// do stuff
    	super.onUserLeaveHint();
    	System.out.println("HOMEEEEEEEEE");
    }
}

When “onUserLeaveHint()” function is defined you can actually listen to the home button press and do some saving of data or something.

 public void onUserLeaveHint() { // this only executes when Home is selected.
    	// do stuff
    	super.onUserLeaveHint();
    	System.out.println("HOMEEEEEEEEE");
    }

Home button override

Home button override

Home button override

Please leave your comments on this post and share it on the social Networks.

5 thoughts on “How to override hardware Home button in android? OR How to listen to home button click in android?

    1. James Post author

      May be google has removed it in the latest versions. However we shouldnot override the HomeButton.

      Reply
      1. Anita Gomber

        No doubt Its working fine with 2.3.3 but not working with jelly bean. If you find something with jelly bean kindly post on the website.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *