Throughout its life, an Activity
can be in one of several states—active (i.e., running), paused or stopped. The Activity
transitions between these states in response to various events:
• An active Activity
is visible on the screen and “has the focus”—that is, it’s in the foreground. This is the Activity
the user is interacting with.
• A paused Activity
is visible on the screen but does not have the focus—such as when an alert dialog is displayed.
• A stopped activity is not visible on the screen and is likely to be killed by the system when its memory is needed. An Activity
is stopped when another Activity
becomes active.
As an Activity
transitions among these states, the Android runtime calls various Activity
lifecycle methods—all of which are defined in the Activity
class
You’ll override the onCreate method in every activity. This method is called by the Android runtime when an Activity
is starting—that is, when its GUI is about to be displayed so that the user can interact with the Activity
. Other lifecycle methods include onStart
, onPause
, onRestart
, onResume
, onStop
and onDestroy
. We’ll discuss most of these in later chapters. Each activity lifecycle method you override must call the superclass’s version; otherwise, an exception will occur. This is required because each lifecycle method in superclass Activity
contains code that must execute in addition to the code you define in your overridden lifecycle methods.