Background:
Android3.0 (API level 11), the Android device no longer needs a special menu key.
With this change, Android apps should eliminate the reliance on traditional 6 menus. Instead, the Anction bar is provided to provide basic user functionality.
Create 3 Basic Menus
Options menu and Action Bar
The Options menu is a feature that has global impact in the app, such as "search", "Send Mail", "set", and so on.
In Android3.0 and above, the Options menu is placed on the action bar.
To create an option menu:
Overriding the Oncreateoptionmenu () method
@Override Public
Boolean oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.menu.game_menu, menu);
return true;
}
There are two ways to add menu items: One is to refer to an XML file and the other is to use the Add () method.
In Android2.3, the system calls the Oncreateoptionmenu () method after the user clicks on the menu key, and at 3.0 and above, the system invokes the Oncreateoptionmenu () method at the beginning of the activity.
To handle a menu item response event:
Call onoptionsitemselected (MenuItem Item) method
@Override Public
Boolean onoptionsitemselected (MenuItem item) {
//Handle Item selection
switch ( Item.getitemid ()) {case
r.id.new_game:
newgame ();
return true;
Case R.ID.HELP:
showHelp ();
return true;
Default: Return
super.onoptionsitemselected (item);
}
Dynamically changing the contents of the Options menu requires the use of the Onprepareoptionsmenu () method, which passes the menu object so that we can manipulate it, such as adding menu items, deleting the item, and so on.
Note that, at 3.0 and above, the Invalidateoptionsmenu () method needs to be invoked before using the Onprepareoptionsmenu method.