To handle the hardware back button, you can use this code (found on How to handle back button in activity)
@Override
public void onBackPressed() {//for api level 5
// your code.
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {//higher than api 5
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Use the first one if you have your minimum API set at level 5 or lower (as far as I can tell that should work)
Now, we need to navigate up. So, where the your code comment is, you should be able to write:
NavUtils.navigateUpFromSameTask(this);
and have it navigate up properly.