In this article we will see how to override a key down action in Android. Android support several Key Events. They are mapped in the KeyEvent class. This object is used to report key and button events like KEYCODE_BACK, KEYCODE_HOME, … The complete list of event is showed in the Android Developer Reference. The simple example below shows how to override the Back Button action in order to call a specific Activity and not the previous one.
public class MyActivity extends ListActivity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// return to the App's Home Activity
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(this, MyBackActivity.class);
this.startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}
}