1

Which of these two ways is the proper way to override onCreateOptionsMenu and why?

Like this, returning a boolean directly

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

Or like this, returning a boolean from the parent method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);
}
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • 1
    check [this](https://stackoverflow.com/questions/10303898/oncreateoptionsmenu-calling-super) – anatoli Jul 13 '17 at 18:07
  • In Android boolean methods are intended to give you flow control. If return true it means the event has being handle, if return false then the event is not handle. If the event is not handle then other events will be executed. By example in longClickListener if return false clickListener will be executed. – cutiko Jul 13 '17 at 18:07
  • 2
    Possible duplicate of [onCreateOptionsMenu() calling super](https://stackoverflow.com/questions/10303898/oncreateoptionsmenu-calling-super) – Abhishek Jain Jul 13 '17 at 18:08
  • Do you want your parent to have a chance to do processing? If so, call it. If not, don't. – Gabe Sechan Jul 13 '17 at 18:10
  • @GabeSechan Sure, but why would I want the parent to do any processing if I've already inflated the menu for the activity? I don't see the point. – the_prole Jul 13 '17 at 20:01
  • @the_prole Depends on your architecture. Does your parent process the menu to change it? For example the base activity class in my current app adds a debug menu if its a debug build. – Gabe Sechan Jul 13 '17 at 20:02
  • @GabeSechan I see, so in your case your inflating and adding an additional menu? – the_prole Jul 13 '17 at 22:12

1 Answers1

0

If you manually inflated the menu with inflater.inflate(), this is fine:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}