1

In my app I have some activities without menu items, that use the following override:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.appbar_menu_empty, menu);
    return true;
}

This works good. If I remove the override, I get the same effect on Android 5.1, i.e. an action bar with no icons.

So the question is: can I drop the override?

The documentation of Activity.onCreateOptionsMenu states:

The default implementation populates the menu with standard system menu items.

What does that mean? Do I need to expect that Android comes up with some buttons I did not explicitly add?

Simon Warta
  • 10,850
  • 5
  • 40
  • 78

3 Answers3

2

You can remove OncreateOptionsMenu() if you dont want to have menu items.

If you want to add menu items, edit the menu.xml file in resources/menu directory.

Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
  • 1
    This is also what I experienced. Is there any reliable source that documents what the default implementation is? – Simon Warta Nov 26 '15 at 11:51
0

From the docs the method is defined in Activity class as below

Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu.

This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(android.view.Menu).

The default implementation populates the menu with standard system menu items. These are placed in the android.view.Menu.CATEGORY_SYSTEM group so that they will be correctly ordered with application-defined menu items. Deriving classes should always call through to the base implementation.

You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.

When you add items to the menu, you can implement the Activity's onOptionsItemSelected(android.view.MenuItem) method to handle them there.

Parameters: menu The options menu in which you place your items. Returns: You must return true for the menu to be displayed; if you return false it will not be shown.

public boolean onCreateOptionsMenu(Menu menu) {
    if (mParent != null) {
        return mParent.onCreateOptionsMenu(menu);
    }
    return true;
}

also check this SO thread out onCreateOptionsMenu() calling super

check the code for Activity class here http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/Activity.java#Activity.onCreateOptionsMenu%28android.view.Menu%29

Community
  • 1
  • 1
karan
  • 8,637
  • 3
  • 41
  • 78
  • Thanks. You're now mixing `onCreateOptionsMenu` and `onPrepareOptionsMenu` in your answer. Could you clarify that? – Simon Warta Nov 26 '15 at 12:10
  • 1
    thats straight from the docs, it is declared and defined in the Activity class which is in android package `android.app`. i.e. super declaration for `onCreateOptionsMenu` – karan Nov 26 '15 at 12:22
  • Okay the link clarifies it. You're copying the "see also" content into the code for onPrepareOptionsMenu. So your answer has basically nothing to do with onCreateOptionsMenu because you looked at a different function. – Simon Warta Nov 26 '15 at 12:29
  • Well, then I do not really know what your answer is trying to tell me. I already pointed to a part of that documentation in my question. The code is nice, thanks. But I still don't know if `mParent.onCreateOptionsMenu` adds items or not. So for now, not helping. – Simon Warta Nov 26 '15 at 13:11
-1

see some sample code here where you need to show option in action bar menu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_act_add_recipe, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

/res/menu/menu_act_add_recipe.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_add_image"
    android:icon="@drawable/ic_tab_add_image_white"
    android:orderInCategory="100"
    android:title="@string/action_preview"
    app:showAsAction="always" />
<item
    android:id="@+id/action_recipe_preview"
    android:icon="@drawable/ic_tab_check_white"
    android:orderInCategory="100"
    android:title="@string/action_preview"
    app:showAsAction="always" />

Suhas Bachewar
  • 1,230
  • 7
  • 21