Save the file. 5. Select the Layout tab to view the visual designer.
118
Part II: Building and Publishing Your First Android Application
Working with methods, stacks, and states
Almost all activities implement these two methods: ✓
onCreate: This is where you initialize your activity. Most importantly, this is where you tell the activity what layout to use using layout resource
identifiers. This can be considered the entry point of your activity. ✓
onPause: This is where you deal with the user leaving your activity. Most importantly, any changes made by the user should be committed
at this point that is, if you need to save them.
Activities in the system are managed as an activity stack. When a new activity is created, it is placed on top of the stack and becomes the running activity.
The previous running activity always remains below it in the stack and does not come to the foreground again until the new activity exits.
I cannot stress enough the importance of understanding how and why the activity works behind the scenes. Not only will you come away with a better
understanding of the Android platform, but you will also be able to accurately troubleshoot why your application is doing very odd things at run time.
An activity has essentially four states, as shown in Table 5-1.
Table 5-1 Four Essential States of an Activity
Activity State Description
Activerunning The activity is in the foreground of the screen at the top of
the stack. Paused
The activity has lost focus but is still visible that is, a new non- full-sized or transparent activity has focus on top of your activ-
ity. A paused activity is completely alive, meaning that it can completely maintain state and member information and remains
attached to the window manager that controls the windows in Android. However, note that the activity can be killed by the
Android system in extreme low-memory conditions.
Stopped If an activity becomes completely obscured by another activ-
ity, it is stopped. It retains all state and member information, but it is not visible to the user. Therefore, the window is hidden
and will often be killed by the Android system when memory is needed elsewhere.
Create and resuming
The system has either paused or stopped the activity. The system can either reclaim the memory by asking it to finish or it
can kill the process. When it displays that activity to the user, it must resume by restarting and restoring to its previous state.
119