0

Can i override the functionality of back and homebuttons(hardware) in android? i mean like clicking on home button should go tohome screen of my app instead of home screen of mobile

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109

3 Answers3

3

Home Buttons:

=> You can't override the behaviour of home button.

Back Buttons:

=> In order to capture or override the default back button press in Android the following onKeyDown method can be implemented by the Activity.

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

In case of Android 2.0+ a convince method is provided as

  @Override
    public void onBackPressed() {

        // implement your override logic here
       return;
    }
Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
confucius
  • 13,127
  • 10
  • 47
  • 66
0

for back Button Override

 public void onBackPressed() {

    // implement your override logic here
   return;
}

for home key : create a home app . refer : Android Overriding home key

Community
  • 1
  • 1
Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • Voted down for wrong method name and quite sloppy content, removed vote after edit. :) – Jave Dec 05 '11 at 10:51
-1
Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);             
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196