How to treat Android’s up button like a back button

When opening up a child screen on most android apps an arrow will appear on the upper left corner of the screen indicating that you can go back to the previous screen. Android’s documentation refers to this as an “up” button (despite the fact that it clearly points left). While at first glance it appears to similar to the back button it is subtly different.

More complex apps may have a use for the difference in this button’s behavior, but for simple apps the up button acts like a back button that additionally blows away minor state information (such as scroll position) on the parent screen. Fortunately there is a simple way to force the up button to act the same as your standard back button.

In the child activity find the onOptionsItemSelected() method…

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

and replace NavUtils.navigateUpFromSameTask(this); with finish(); so that your new code now looks like...

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Now the back button on your action bar will work just as well as the back button at the bottom of the screen.