Should “android: onOptionsItemSelected” return true or false

后端 未结 5 542
情话喂你
情话喂你 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: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;
    }
    

提交回复
热议问题