首页 > 代码库 > Starting an Activity 开启一个活动
Starting an Activity 开启一个活动
Starting an Activity
开启一个活动
This lesson teaches you to
这个课程教你去了解以下:
- Understand the Lifecycle Callbacks 理解生命周期回调
- Specify Your App‘s Launcher Activity 指定应用程序的启动活动
- Create a New Instance 创建一个新的案例
- Destroy the Activity 销毁这个活动
You should also read
- Activities
Try it out
ActivityLifecycle.zip
Unlike other programming paradigms in which apps are launched with a main()
method, the Android system initiates code in anActivity
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.
与其他的编程范式即应用程序带有一个main()方法的范式不同,Android系统在特定的生命周期阶段通过调用特定的回调方法在一个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.
当用户开始离开这个活动,系统调用其他方法将活动状态的活动掉回到金字塔来拆分活动。在某些情况下,该活动将只沿着金字塔移动一部分或者等待(如当用户切换到另一个应用程序),这个点的活动可以从用户离开离开的地方返回顶部(如果用户返回到活动)和继续。
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.
在横向和纵向之间当屏幕旋转方向时,不崩溃或失去用户的进程。