0

Context: Very familiar with iOS development and Java. New to Android.

Problem: I added a handler to navigate 'back' when the home button is pressed. It works fine, until I add another handler when the user scan button:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (item.getItemId() == R.id.home) //The ID here is no longer R.id.home
    {
        navigateBack();
        return true;
    }
    else if (item.getItemId() == R.id.scan_button)
    {
        presentScanner();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

As soon as I add the code for the Scan the id is no longer R.id.home. It works if I do this:

    if (item.getItemId() == 16908332)
    { . . etc. . . 

What has happened? How can I correctly get the ID of the home button in this current activity?

Update: Changed from R.id.home to 'android.R.id.home' and this works. Why?

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185

1 Answers1

1

As pre @Romain Guy the man behind android this cannot be done see here. But for older devices you might want to try the keyevent method.. This post explores additional ways to achieve what you are looking for.

UPDATE android.R.id.home is used in the actionbar to know that your actionappIcon is pressed(left side). While R.id.home is the reference to physical home button(or touch as in nexus 5). Have a look at this tutorial for further explanation and use cases.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61