What is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio?
-
In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?
-
In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?
Why not call the super class method first, and if it returns
false
, bail early? Eg:public boolean onPrepareOptionsMenu(Menu menu) {
if (!super.onPrepareOptionsMenu(menu))
{
return false;
}menu.findItem(R.id.action_save).setVisible(true);
menu.findItem(R.id.action_reset).setVisible(true);
menu.findItem(R.id.action_about).setVisible(true);
return true;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Why not call the super class method first, and if it returns
false
, bail early? Eg:public boolean onPrepareOptionsMenu(Menu menu) {
if (!super.onPrepareOptionsMenu(menu))
{
return false;
}menu.findItem(R.id.action_save).setVisible(true);
menu.findItem(R.id.action_reset).setVisible(true);
menu.findItem(R.id.action_about).setVisible(true);
return true;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for your suggestion. I'll keep that approach in mind for future reference. However, I found a better way to deal with the situation. Unless my code relies on a superclass's implementation, returning ``true`` after my modifications is straightforward. If I ever extend a class and want to allow the superclass's logic to still apply, I'll use the ``super`` call then. That seems to be the optimal approach.
-
In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?
To maintain consistency and ensure proper functionality, follow these guidelines when implementing onCreateOptionsMenu() and onPrepareOptionsMenu() in an AppCompatActivity:
onCreateOptionsMenu()
Use the first example where you return true after inflating the menu. This is the recommended approach because super.onCreateOptionsMenu(menu) in AppCompatActivity always returns true by default and doesn't perform any additional actions unless overridden.@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
onPrepareOptionsMenu()
Use the second example where you call super.onPrepareOptionsMenu(menu) first and return true later. This ensures any default behavior is executed, and returning true indicates the menu was successfully updated.@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_save).setVisible(true);
menu.findItem(R.id.action_reset).setVisible(true);
menu.findItem(R.id.action_about).setVisible(true);
return true;
}