226 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus
this is what we are doing in this chapter with your EditGalaxy Activity subclass, since we only want
your MainActivity class Menu object to be able to start this private component of your Hello Universe
application.
Finally, remember one important “rule” regarding the use of Intent objects in your Android
programming! To ensure that your Android application is completely secure, you will always want to
use an explicit Intent object when starting a Service object (Service subclass). This also means that
you would not want to declare
application’s Service subclasses, since these are only utilized with implicit Intent object processing,
and are not needed when using explicit Intent objects.
The reason for this “rule” is because by using an implicit Intent object to start a background
processing Service class, you risk creating a security hazard within the Android OS on your end
user’s device. This is because the developer cannot ascertain what Service subclass will respond to
an implicit Intent object, and the end user also cannot see which Service is starting on their Android
device. This opens up the ability for a destructive Service to be created, which could be triggered
by an implicit Intent object used to start a Service subclass. So don’t create Services using implicit
Intent objects!
Instantiate an Intent Object: An App’s Context
Let’s instantiate, name, and configure an explicit Intent object named editIntent, which will start the
EditGalaxy Activity subclass, which we are going to create as the next step in this work process.
This is done by declaring the Intent object for use, then providing a name, and then “loading” the
newly named Intent object using the equal sign, while at the same time (in the same Java statement)
creating an Intent using the Java new keyword, and configuring this new Intent with the class name
which you will be starting.
All of this can be accomplished by using the following single line of Java programming logic:
Intent editIntent = new Intent( this, EditGalaxy.class );
So the Intent declares the object type, the editIntent gives it a name, the equals = sign signifies
that we are about to instantiate it, the Java new keyword creates an instance of the Intent object,
and finally the parameter list passes over the Context object for our MainActivity Activity subclass,
using the Java this keyword, as well as the EditGalaxy.class Activity subclass that we want to
start or launch using the explicit Intent object created with this Java statement. As you can see in
Figure 7-9, we have some wavy red error underline highlighting in the code, which you will probably
guess is related to creating either import statements or code structures which do not yet exist, but
which are referenced within the Java code structure you are currently coding (you have seen both of
these error scenarios before).