Should "android: onOptionsItemSelected" return true or false

In onOptionsItemSelected... I saw some code that are different in the switch block.

Case 1 (Normally seen)

public boolean onOptionsItemSelected (MenueItem item)
       switch (item.getItemId()){
             case R.id.item1:
             startActivity (new Intent (this, PrefsActivity.class));
             break;
       }
       return true

Case 2 (unsure of why it's set up this way)

public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
               case MENU_NEW_GAME:
               newGame();
               return true;
       }
       return false;

My Question

What are the differences between Case 1 and Case 2?


Solution 1:

Per the documentation for onOptionsItemSelected()

Returns

boolean Return false to allow normal menu processing to proceed, true to consume it here.

The if returned true the click event will be consumed by the onOptionsItemSelect() call and won't fall through to other item click functions. If your return false it may check the ID of the event in other item selection functions.

Your method will still work, but may result in unnecessary calls to other functions. The ID will ultimately fall through those functions since there is no switch to catch it, but return false is more correct.

Solution 2:

As per documentation
true --> Event Consumed here, now It should not be forwarded for other event
false --> Forward for other event to get consumed

This boolean return type actually benefits when we are working with multiple fragments and every fragment has their own Options menu and override OnOptionItemSelected(Mainly in tablet design).

In this case android trace every fragment's OnOptionItemSelected() method, to avoid that

a) If any fragment is consuming event in onOptionsItemSelected() return "true"(to stop) else return "false"
b) If We return false then It will trace other connected fragment's onOptionsItemSelected()
method until it ends all fragment or somebody consumes It.

enter image description here

Here I have tried to explain from diagram
Green color boundary is fragment-1 and Red color boundary is fragment-2
both fragment has their own Optionmenu which I have highlighted

Now If we click any of OptionmenuItem It will check Implementation of onOptionsItemSelected() in both fragments

If any fragment is consuming event onOptionsItemSelected return true, By this it would never try for other fragment and we can reduce overhead of Android operation system.

Solution 3:

When I used Android Studio to generate a generic app, the template code for onOptionsItemSelected() returns true if item consumed otherwise it passes the call onto the super class.

@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_mymenuaction) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Solution 4:

The problem with your method is that you return true even if your switch statement does not find an item. If you return true immediately like the other method for each switch case, then you can assume, if you are at the end of the method, that no switch cases were found, so return false to show that it was not handled.

Solution 5:

I just had the problem that my

getActionBar().setDisplayHomeAsUpEnabled(true);

was not working. When touching the back button it would be highlighted but nothing happened.

It took me a while to figure out that this was the return of true.

In my opinion the best solution with less code duplication would be the following:

public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
       case MENU_NEW_GAME:
           newGame();
           break;
       default:
           return false;
   }
   return true;
}