The Android App Activity Lifecycle
An activity is a single, focused action that a user can take. You can think of an activity like a “page” in your Android app. For example, an activity might present a list of menu items that a user can choose from, or it might display photographs along with captions. An app may consist of only one activity or (more commonly) several.
Though activities may work together to appear to be one cohesive application, they work independently from each other. Almost all activities interact with the user, so the Activity class creates for you the window in which you can place your user interface (UI).
An activity in Android is an important part of an application’s overall lifecycle, and the way the activities are launched and put together is a fundamental aspect of the Android application model. Every activity is implemented as a subclass of the Activity base class.
The Activity lifecycle is one of the most important differences between Android and other phone operating systems. It’s complicated, but it’s an important set of concepts to grasp before you dive into developing Android apps.
Two important methods that almost all activities implement are
onCreate: Where the activity is initialized. Most importantly, it’s where you tell the activity which layout to use by using a layout resource identifier — considered the entry point of your activity.
onPause: Where you deal with the user leaving your activity. Any changes made by the user should be committed at this point (if you need to save them).
Activities in the system are managed as an activity stack. When a new activity is created, it’s placed on top of the stack and becomes the running activity. The previous running activity always remains below it in the stack and returns to the foreground only when the new activity exits.
To be a successful Android programmer, you must understand the importance of how and why the activity works behind the scenes. This will make you a better Android programmer and help you debug strange problems later.
An activity essentially has four states.
|Active/running||The activity is in the foreground of the screen (at the top of
|Paused||The activity has lost focus but is still visible. (A new,
non-full-size or transparent activity has the focus on top of your
activity.) Because a paused activity is completely alive, it can
maintain state and member information and remains attached to the
window manager in Android.
|Stopped||If an activity becomes obscured by another activity, it is
stopped. It retains all state and member information, but
isn’t visible to the user. Therefore, the window is hidden
and will often be killed by the Android system when memory is
|Destroyed||When the activity is paused or stopped, the system can reclaim
the memory by asking it to finish, or it can kill the process. When
it displays the activity again to the user, it must be completely
restarted and restored to its previous state.
The figure shows the important paths of an activity — the activity lifecycle.
The rectangles represent callback methods you can implement to respond to events in the activity. The shaded ovals represent the major states of the activity.