本节要点:
Activity的生命周期。
//=========================================================================================
Understand the Lifecycle Callbacks
During the life of an activity, the system calls a core set of lifecycle methods in a sequence similar to a step pyramid. That is, each stage of the activity lifecycle is a separate step on the pyramid. As the system creates a new activity instance, each callback method moves the activity state one step toward the top. The top of the pyramid is the point at which the activity is running in the foreground and the user can interact with it.
As the user begins to leave the activity, the system calls other methods that move the activity state back down the pyramid in order to dismantle the activity. In some cases, the activity will move only part way down the pyramid and wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off.
金字塔型如下图:
- Does not crash if the user receives a phone call or switches to another app while using your app.
- Does not consume valuable system resources when the user is not actively using it.
- Does not lose the user's progress if they leave your app and return to it at a later time.
- Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
As you'll learn in the following lessons, there are several situations in which an activity transitions between different states that are illustrated in figure 1. However, only three of these states can be static. That is, the activity can exist in one of only three states for an extended period of time:
-
Resumed
- In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.) Paused
- In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code. Stopped
- In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
onCreate()
, it quickly callsonStart()
, which is quickly followed by onResume()
.Create a New Instance
Activity
by calling its
onCreate()
method.
You must implement the onCreate()
method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate()
should define the user interface and possibly instantiate some class-scope variables.
onCreate()
finishes execution, the system calls the
onStart()
and
onResume()
methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when
onStart()
is called, but
onResume()
quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.
onDestroy()
after it has already called onPause()
and onStop()
in all situations except one: when you call finish()
from within the onCreate()
method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish()
from withinonCreate()
to destroy the activity. In this case, the system immediately calls onDestroy()
without calling any of the other lifecycle methods.