222 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus
Since the onOptionsItemSelected( ) method evaluates MenuItem objects, you will need to declare a
MenuItem and name it item inside of the method parameter list area, because you will want to pass
(or take) in MenuItem objects for evaluation, and inside of the method you are going to reference
these using the name item. This is done using the following method declaration construct, as is
shown in Figure 7-7:
public boolean onOptionsItemSelected( MenuItem item ) { return false; }
Figure 7-7. Add a MenuItem object named item to the onOptionsItemSelected( ) method, then import the MenuItem class
Mouse-over the wavy red error highlighting under the MenuItem class reference and select the
Import ‘MenuItem’ (android.view) option to have Eclipse write the Java import statement for you.
Now that the method infrastructure is in place, we need to write Java code inside of the method that
looks at the MenuItem object (named item) which is passed into the method, and puts this MenuItem
object through some sort of evaluation structure, to ascertain what to do if the MenuItem conforms
to certain characteristics—in this case, its ID.
The Java Switch Statement: Choosing a MenuItem
The Java programming structure to use in this scenario is called a “switch” structure, which will
allow the Java code to switch between different code scenarios, based on a “case”-by-case
evaluation structure, which you put into place. We are going to take a closer look at how to put
together this type of Java code evaluation structure in this section of the chapter.