Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating R.id.home

I was following this tutorial here - http://developer.android.com/training/implementing-navigation/ancestral.html - for implementing the Up Navigation. Sort of the same as the user pressing the Back button on the phone, but the onBackPressed() method does not fire when the 'Up' button is pressed. In the tutorial they show that you trap R.id.home in the onOptionsItemSelected() method. This web page - http://developer.android.com/reference/android/R.id.html - shows that the value of R.id.home should equal 16908332, but it doesn't in my app. In the code below if I use R.id.home it fails. If I hard-code in 16908332 it works. For me R.id.home evaluates to 21312330724. According to the page, all of the R.id values begin 1690. I hate hard-coding in a value for a built-in value, but I'm not sure what else to do. Could this cause problems down the road? Am I doing something wrong? Is this a bug?

Greg

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    //noinspection SimplifiableIfStatement
    if (id == R.id.home) {//16908332
        Intent upIntent = NavUtils.getParentActivityIntent(this);

        upIntent.putExtra(CAT_ID, CatID);
        upIntent.putExtra(USER_ID, UserID);
        upIntent.putExtra(LIST_ID, ListID);
        setResult(RESULT_OK, upIntent);
        NavUtils.navigateUpTo(this, upIntent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}
like image 748
user1091524 Avatar asked Dec 28 '14 23:12

user1091524


1 Answers

You need to compare it to the android one:

if (id == android.R.id.home){
    ...
}
like image 131
MarcSB Avatar answered Oct 24 '22 03:10

MarcSB