Question : What two methods you have to override when implementing Android option menus?
1. onCreateOptionsMenu, onCreateContextMenu
2. onCreateContextMenu, onContextItemSelected
3. onCreateOptionsMenu, onOptionsItemSelected
4. onCreateOptionsMenu, onContextItemSelected
Correct Answer : 3 Explanation: Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.
To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide their own onCreateOptionsMenu() callback). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
Question : Which of the following is a call-back method that inflates an options menu from file res/menu/menu.xml?
1. onOptionsItemSelected
2. onCreate
3. onCreateMenu
4. onCreateOptionsMenu
Correct Answer : 4 Explanation: You can declare items for the options menu from either your Activity subclass or a Fragment subclass. If both your activity and fragment(s) declare items for the options menu, they are combined in the UI. The activity's items appear first, followed by those of each fragment in the order in which each fragment is added to the activity. If necessary, you can re-order the menu items with the android:orderInCategory attribute in each you need to move.
To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide their own onCreateOptionsMenu() callback). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } You can also add menu items using add() and retrieve items with findItem() to revise their properties with MenuItem APIs.
Question : Which of the following Activity methods is invoked when the user clicks on an options menu item?
1. onItemClicked
2. onItemSelected
3. onOptionsItemClicked
4. onOptionsItemSelected
Correct Answer : 4 Explanation: When the user selects an item from the options menu (including action items in the app bar), the system calls your activity's onOptionsItemSelected() method. This method passes the MenuItem selected. You can identify the item by calling getItemId(), which returns the unique ID for the menu item (defined by the android:id attribute in the menu resource or with an integer given to the add() method). You can match this ID against known menu items to perform the appropriate action. For example:
@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); } } When you successfully handle a menu item, return true. If you don't handle the menu item, you should call the superclass implementation of onOptionsItemSelected() (the default implementation returns false).
If your activity includes fragments, the system first calls onOptionsItemSelected() for the activity then for each fragment (in the order each fragment was added) until one returns true or all fragments have been called.