Should “android: onOptionsItemSelected” return true or false

后端 未结 5 524
情话喂你
情话喂你 2020-12-07 20:59

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

Case 1 (Normally seen)

public boolean onOptionsI         


        
相关标签:
5条回答
  • 2020-12-07 21:15

    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);
    }
    
    0 讨论(0)
  • 2020-12-07 21:21

    kleaver,

    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.

    0 讨论(0)
  • 2020-12-07 21:24

    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.

    0 讨论(0)
  • 2020-12-07 21:28

    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.

    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.

    0 讨论(0)
  • 2020-12-07 21:29

    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;
    }
    
    0 讨论(0)
提交回复
热议问题