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,292
  • 9
  • 58
  • 106

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,220
  • 3
  • 35
  • 45
confucius
  • 12,867
  • 10
  • 46
  • 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,032
  • 3
  • 32
  • 39
  • 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
  • 20,719
  • 39
  • 122
  • 185