Should "android: onOptionsItemSelected" return true or false

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkleaverView Question on Stackoverflow
Solution 1 - AndroidWill TateView Answer on Stackoverflow
Solution 2 - AndroidLokesh TiwariView Answer on Stackoverflow
Solution 3 - AndroiddbagnaraView Answer on Stackoverflow
Solution 4 - Androidcoder_timView Answer on Stackoverflow
Solution 5 - AndroidbechtoldView Answer on Stackoverflow