CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 231
Before we start working on building out your EditGalaxy.java Activity subclass’s Java code, let’s
finish up with coding the MenuItem switch and case statements we have been building first, so
that we finish up with what we are learning about in this part of the chapter: how to create a working
options menu inside of the MainActivity Activity subclass. The next step is adding a method call to
the .startActivity( ) method, passing over the Intent we just created.
Starting an Activity: Using the .startActivity( ) Method
As you can see in Figure 7-13, now that the EditGalaxy.java Activity subclass exists, the error
highlighting has vanished, and you are ready to call the .startActivity( ) method from your
MainActivity subclass. This is done “off of,” or using, your Activity subclass’s Context object,
which is named (using a Java this keyword “shortcut") this. Within the method you pass over the
editIntent (explicit) Intent object which you created and configured in the previous line of Java code.
This Intent passing construct is done using the following line of Java code:
this.startActivity(editIntent);
Figure 7-13. Calling the .startActivity( ) method off of the current Context object, and passing over the editIntent Intent object
This will call the android.app.Activity package’s .startActivity( ) method, which is, as you can see, part
of the Android Activity class, off of the current Context (this) object, and passes over as a parameter
the editIntent Intent object. This will start your new Activity subclass (EditGalaxy.java, which will
become EditGalaxy.class).
Figure 7-14 shows the completed onOptionsItemSelected( ) method structure, with the new
edit_galaxy case statement and the action_settings case statement that was created by the Eclipse
New Android Application Project series of dialogs. The default statement is triggered if none of the
case statements are called (utilized) and passes (returns) control of the MenuItem Object back up to
the superclass using the super keyword and the method name and item Object using the statement
return super.onOptionsItemSelected(item); otherwise a return true; statement is used if any of the
case statements were evaluated and completed.