首页 > 代码库 > Activity的生命周期
Activity的生命周期
Managing the Activity Lifecycle
DEPENDENCIES AND PREREQUISITES
- How to create an Android project (seeCreating an Android Project)
YOU SHOULD ALSO READ
- Activities
TRY IT OUT
ActivityLifecycle.zip
As a user navigates through, out of, and back to your app, theActivity
instances in your app transition between different states in their lifecycle. For instance, when your activity starts for the first time, it comes to the foreground of the system and receives user focus. During this process, the Android system calls a series of lifecycle methods on the activity in which you set up the user interface and other components. If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact).
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, if you‘re building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.
This class explains important lifecycle callback methods that each Activity
instance receives and how you can use them so your activity does what the user expects and does not consume system resources when your activity doesn‘t need them.
Lessons
- Starting an Activity
- Learn the basics about the activity lifecycle, how the user can launch your app, and how to perform basic activity creation.
- Pausing and Resuming an Activity
- Learn what happens when your activity is paused (partially obscured) and resumed and what you should do during these state changes.
- Stopping and Restarting an Activity
- Learn what happens when the user completely leaves your activity and returns to it.
- Recreating an Activity
- Learn what happens when your activity is destroyed and how you can rebuild the activity state when necessary.
管理活动的生命周期
依赖和先决条件
- 如何创建一个Android项目(见吗创建一个Android项目)
你也应该读
- 活动
试一试
下载演示ActivityLifecycle.zip
用户通过导航,走出,回到你的应用,
Activity
实例应用程序生命周期的不同状态之间的过渡。例如,当你的活动第一次开始,它转到系统的前面并接收用户的焦点。在这个过程中,Android系统调用一系列设置用户界面和其他组件的活动的生命周期方法。如果用户执行一个动作,开始另一个活动或切换到另一个应用程序,系统在你的活动中调用另一组生命周期方法,因为它进入后台(活动不再是可见的,但实例及其状态保持不变)。在生命周期回调方法中,当用户离开和重新进入活动时您可以声明你的活动如何进行。例如,如果您正在构建一个流媒体视频播放器,当用户切换到另一个应用程序时,你可能会暂停视频、终止网络连接。当用户返回时,你可以重新连接到网络,允许用户从同一地点恢复视频。
这个类解释了每个重要的生命周期回调方法
,Activity
实例接收和如何使用它们,这样你的活动可以满足用户所期望,当你的活动不需要他们时也不消耗系统资源。教训
- 开始一个活动
- 基本了解活动的生命周期,用户可以启动应用程序,以及如何执行基本的创造活动。
- 暂停和恢复活动
- 了解发生了什么当你的活动暂停(部分被遮挡)和恢复,你应该做的是在这些状态改变。
- 停止和重新启动一个活动
- 了解当用户完全离开你的活动并返回它。
- 重新创建一个活动
- 了解会发生什么当你的活动是摧毁,必要时如何重建活动状态。