Override Android Back Button

ryandlf picture ryandlf · Sep 26, 2011 · Viewed 9.3k times · Source

A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.

Is there a way to set the back button to go to a specific activity rather than killing the current activity window?

Answer

Tyler Treat picture Tyler Treat · Sep 26, 2011

I believe you should be able to do something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        // start new Activity here
    }
    return super.onKeyDown(keyCode, event);
}

But overriding the expected functionality of the back button is not advisable.