2

I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.

I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back. Thanks.

vickmwas
  • 56
  • 5

2 Answers2

3

You can do this.

@Override
public void onBackPressed() {
    NavUtils.navigateUpFromSameTask(this);
}
phoenix
  • 496
  • 3
  • 11
0

You can do this

Overide onOptionsItems Selected

Create a switch case of android.R.id.home Call method onBackPressed();

here is the code

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();

            default:
                return super.onOptionsItemSelected(item);
        }
    }
Abednego
  • 599
  • 10
  • 17