@Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case R.id.menu_item_one: // handle menu item one click return true; case R.id.menu_item_two: // handle menu item two click return true; default: return super.onOptionsItemSelected(item); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home){ onBackPressed(); return true; } return super.onOptionsItemSelected(item); }In this example, we are checking if the selected menu item has the ID of android.R.id.home, which represents the back arrow button in the action bar. If the back arrow button is clicked, we are calling the onBackPressed() method to go back to the previous activity. We are returning true to indicate that we have handled the menu item selection event. This example is most likely from the Android Support Library, which provides backward-compatible versions of Android framework components.