Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionbar home/up button actual id differs from R.id.home

I have a ridiculous problem! I want to handle action home button, but MenuItem passed to onOptionsItemSelected method when i tap 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?

like image 448
MamadTell Avatar asked Feb 08 '23 12:02

MamadTell


2 Answers

You are missing the 'android' prefix. The Up button's id is android.R.id.home actually, not R.id.home.

like image 155
Mohammed Aouf Zouag Avatar answered Feb 16 '23 04:02

Mohammed Aouf Zouag


You handle home button like

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id==android.R.id.home){
         /*Do your code here*/
    }
}
like image 38
Collins Abitekaniza Avatar answered Feb 16 '23 03:02

Collins Abitekaniza