A (175)

(Tuis.) #1
CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 227

Let’s take care of the import statement for the Intent class first. Mouse-over the Intent error
highlighting, either one will provide the same pop-up helper dialog, and select the Import ‘Intent’
(android.content) option. Next, we will go over what the Java this keyword and a Context object
do for your Android application, and then we will continue our work process in the section after that,
and allow Eclipse to create our EditGalaxy.java class.


Explaining Context: The Android Context Class


The concept of Context in Java programming is simple in its definition, and yet complex in its
implementation. It is something that you will need to know what it is doing, and why it is needed, to
be able to accomplish certain things in Android. It is not something you need to manipulate directly,
at least not until you become an advanced Android developer.


The Context object, and the Java this keyword, which essentially passes over a Context object, is
exactly what it sounds like it is! Android Context objects contain detailed “contextual” information
regarding the application component which is calling over to the other application component to do
something. If someone asks you to do something for them, you usually ask them what the context
for the task is, so that you know what, and why, you are doing the task!


The reason for giving the receiving application component this “Context” (clever Java keyword pun
intended) is that Context objects will allow a called application component to “see” or ascertain what
the calling application component is doing, so that it can do the job (task) that it is being asked to do
more efficiently, effectively and with greater precision.


When this “Context” object, which is filled or “populated” with information regarding the calling
application component, is combined with the (inherent) “knowledge” of what the called application
component is doing, it results in “Context,” which, as we all know from real life, can be defined as
the clear, overall view of the entire process which we are currently involved with. A Context object
is similar to a Bundle object, in that it contains a lot of related information, all collected, or bundled
together, into one complex object and sub-object hierarchy.


Figure 7-9. Declaring an Intent object named editIntent and instantiating it using the new keyword and loading it

Free download pdf