Java – Actionbar home/up button actual id is different from R.id.home

Actionbar home/up button actual id is different from R.id.home… here is a solution to the problem.

Actionbar home/up button actual id is different from R.id.home

I have a ridiculous question! I want to handle manipulating the home button, but the MenuItem passed to the onOptionsItemSelected method when I click the home button is not R.id.home (nor R.id.homeAsUp).

    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    Log.v("1111", "MenuItem id: "+id);
    Log.v("1111", "home id: "+R.id.home);
    Log.v("1111", "homeAsUp id: "+R.id.homeAsUp);
    ...
    }

Log:

11-19 23:58:56.433: V/1111(30059): MenuItem id: 16908332
11-19 23:58:56.433: V/1111(30059): home id: 2131296256
11-19 23:58:56.433: V/1111(30059): homeAsUp id: 2131296270

Any ideas?

Solution

You are missing the “android” prefix. The ID of the up button is actually Android. R.id.home, not R.id.home.

Related Problems and Solutions