Philip
Philip

Reputation: 1078

Going back with ActionBar

The following code only works when i set android.R.id.home Does anyone know why? I think a few minutes ago it worked without..

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == android.R.id.home) {
        finish();
    }

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 0

Views: 95

Answers (1)

fweigl
fweigl

Reputation: 22028

The ID android.R.id.home is set automatically by android to the up button. I cannot image that it could have worked with another ID, given you haven't added custom views to your ActionBar or stuff like that.

All IDs on R.id.* are the ones you assigned yourself, the ones in android.R.id.* are predefined IDs by android for some basic stuff (for example the ListView in a ListActivity would have the ID android.R.id.list)

The code you posted is pretty much standard and absolutely correct.

Upvotes: 1

Related Questions