Starting an Activity

  次阅读 来源:互联网(转载协议) 2016-01-14 10:43 我要评论(0)

Unlike other programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.

This lesson provides an overview of the most important lifecycle methods and shows you how to handle the first lifecycle callback that creates a new instance of your 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.

Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there's a callback method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.

Depending on the complexity of your activity, you probably don't need to implement all the lifecycle methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect. Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it:

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:


In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)


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.


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.

The other states (Created and Started) are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method. That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().

That's it for the basic activity lifecycle. Now you'll start learning about some of the specific lifecycle behaviors.


  • 汤晓鸥:人工智能的中国式文艺复兴


  • 区块链最近太火,但A股多家公司明确表态不考虑涉足


  • 马云的新零售?张近东的智慧零售?抑或是刘强东的第四次零售革命?


  • 全球陷入"区块链狂热"潮,大规模应用还需等待



算法 任何一个入门的软件开发人员都会听到这句话:算法是软件的灵魂!近年来大火的机器学习即是在算法在人工智能上的集中体现。今日头条通...



在查获现场,代售点电脑旁的一台机器人通过敲击电脑键盘抢票。 广州日报惠州讯 (全媒体记者秦仲阳 通讯员张中铃摄影报道)2018年春运售票已...