Game Development Reference
In-Depth Information
Figure 4-5. The LogCat output of LifeCycleTest
Pressing the back button again invokes the onPause() method. As it also destroys the activity,
the if if-statement in onPause() also gets triggered, informing us that this is the last we'll see of
that activity.
That is the activity life cycle, demystified and simplified for our game programming needs. We
now can easily handle any pause and resume events, and we are guaranteed to be notified when
the activity is destroyed.
Input Device Handling
As discussed in previous chapters, we can get information from many different input devices on
Android. In this section, we'll discuss three of the most relevant input devices on Android and
how to work with them: the touchscreen, the keyboard, the accelerometer and the compass.
Getting (Multi-)Touch Events
The touchscreen is probably the most important way to get input from the user. Until Android
version 2.0, the API only supported processing single-finger touch events. Multitouch was
introduced in Android 2.0 (SDK version 5). The multitouch event reporting was tagged onto
the single-touch API, with some mixed results in usability. We'll first investigate handling
single-touch events, which are available on all Android versions.
Processing Single-Touch Events
When we processed taps on a button in Chapter 2, we saw that listener interfaces are the way
Android reports events to us. Touch events are no different. Touch events are passed to an
OnTouchListener interface implementation that we register with a View . The OnTouchListener
interface has only a single method:
public abstract boolean onTouch (View v, MotionEvent event)
The first argument is the View to which the touch events get dispatched. The second argument is
what we'll dissect to get the touch event.
 
Search WWH ::




Custom Search