// Oncreateoptionsmenu is called once when the system starts.
@ Override
Public Boolean oncreateoptionsmenu (menu)
{
Menu. Add (0, 0, 0, R. String. menu_about). seticon (Android. R. drawable. ic_menu_info_details );
Menu. Add (0, 1, 1, R. String. menu_exit). seticon (Android. R. drawable. ic_menu_directions ctions );
Return true;
}
/**
* Onoptionsitemselected calls this unified processing function whenever the menu is selected.
* Process menu events
*/
@ Override
Public Boolean onoptionsitemselected (menuitem item)
{
Switch (item. getitemid ())
{
Case 0: // display information
Break;
Case 1: // end program Processing
Smssend. This. Finish ();
Break;
}
Return true;
}
/**
* Onprepareoptionsmenu calls this function midway through or when you need to modify the content of the menu item in the new activity.
* Process menu events
*/
@ Override
Public Boolean onprepareoptionsmenu (menu)
{
Menu. Clear ();
Menu. Add (0, menuexit, 1, "Test 2 ")
. Seticon (Android. R. drawable. ic_menu_directions ctions );
Return super. onprepareoptionsmenu (menu );
}
If there are multiple activities in the program, you can first create a base class of the activity, in this new base class, put the above menu functions.
The subsequent activity forms inherit the base class, so that the menu module can be shared.
If you need to define different menu content from the main menu in an activity, you can use the onprepareoptionsmenu function to reconstruct the menu, because oncreateoptionsmenu is called only once.