Menu processing in multiple Android activities

Source: Internet
Author: User

// 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.