Should "android: onOptionsItemSelected" return true or false
Asked Answered
K

5

64

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?

Kannada answered 19/4, 2011 at 16:3 Comment(0)
F
63

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.

Forge answered 19/4, 2011 at 16:8 Comment(0)
E
49

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.

Evaleen answered 12/11, 2015 at 20:17 Comment(1)
what's the default value returned from super.onOptionsItemSelected(item)?Glengarry
W
2

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);
}
Warrantable answered 24/3, 2016 at 20:59 Comment(1)
i agree with the concept, but you should avoid multiple return statements. It reduces readability. Better to pass "result" boolean field around and exit only at the end... one entry/one exit.Spirochaetosis
A
1

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.

Andromada answered 19/4, 2011 at 16:29 Comment(0)
M
1

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;
}
Moyers answered 14/1, 2015 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.