Handling Menu Selections

Android handles all of an Activity's Menu Item selections using a single event handler, the onOptionsItemSelected method. The Menu Item selected is passed in to this method as the MenuItem parameter.

To react to the menu selection, compare the item.getItemId value to the Menu Item identifiers you used when populating the Menu, and react accordingly, as shown in Listing 4-28.

LISTING 4-28: Handling Menu Item selections Available for download on public boolean onOptionsItemSelected(MenuItem item) { Wrox.com super.onOptionsItemSelected(item);

continues

LISTING 4-28 (continued)

// Find which menu item has been selected switch (item.getitemid()) {

// Check for each known menu item case (MENU_iTEM):

[ ... Perform menu handler actions ... ] return true;

// Return false if you have not handled the menu item. return false;

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment