Hitesh Matnani
Hitesh Matnani

Reputation: 553

How can i set back navigation button in my action bar?

I am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly. Is there any way to code on a button such that it work same as back navigation button of phone?

Upvotes: 0

Views: 349

Answers (2)

Spurdow
Spurdow

Reputation: 2043

I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case android.R.id.home:
                finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

and ofcourse:

actionBar.setDisplayHomeAsUpEnabled(true);

You may/may not used this, its up to you :)

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Along with

actionBar.setDisplayHomeAsUpEnabled(true);

or

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

you can try using this in Activity, too:

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

So your menu item "Home"'s click will be handled using that.

Upvotes: 2

Related Questions