设计 开发 分发
Android 培训 API指南 参考 工具
应用程序组件
Activities
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
一个Activity是一个应用程序组件,提供一个屏幕,用户可以交互来做一些事情;比如:打电话,拍照,发电子邮件,或者查看地图。每一个活动都被给予了一个窗口来描绘它的用户界面。窗口通常会占据整个屏幕,但也可能小于屏幕,或飘于其它窗口的上面。
An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes. (The back stack is discussed more in the Tasks and Back Stack document.)
一个应用程序通常有许多 活动 组成以至于它们是彼此 宽松的限制。通常,一个 活动 应用程序被指定为 主要活动,这是提供给用户 当第一次启动应用程序的时候。每一个活动就可以启动另外一个活动来执行不同的操作。每次一个新的活动开始,前面的活动停止,但系统保存一个活动在一个堆中(返回堆栈)。当一个新的活动开始,它被推到返回堆栈和需要用户的焦点。返回堆栈的基本守则是:后进先出 堆栈机制,因此,当用户完成当前活动,并按下返回按钮时,它从堆栈中弹出(和摧毁)和重新获得前面活动的动作。(返回堆栈在后面的返回堆栈文档中谈论的更多)
When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods. There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change. For instance, when stopped, your activity should release any large objects, such as network or database connections. When the activity resumes, you can reacquire the necessary resources and resume actions that were interrupted. These state transitions are all part of the activity lifecycle.
当一个活动停止由于一个新的活动开始,它被告知这个改变状态通过活动的生命周期回调方法。有几个回调方法,一个活动可以收到几个,由于一个状态的改变,是否系统创建它,阻止他,恢复它,或摧毁它----和每一个回调方法都提供你机会去执行特定工作的恰当状态改变。例如,当你活动停止的时候,你的活动应该释放所有的对象,如:网络,数据库连接。当活动恢复的时候,你可以重拾哪些必要的资源,和恢复动作以至于被打断。这些状态的改变都是所有部分的生命周期。
The rest of this document discusses the basics of how to build and use an activity, including a complete discussion of how the activity lifecycle works, so you can properly manage the transition between various activity states.
本文的其余部分讨论了基本的如何构建和使用一个Activity,包含一个完整的生命的周期的讨论活动如何工作,所以你可以适当的管理各种活动状态之间的转换。
Creating an Activity
To create an activity, you must create a subclass of Activity (or an existing subclass of it). In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are:
创建一个活动,你必须创建一个子类Activity(或者一个现有的子类)。在你的子类中,你需要实现回调方法以至于系统回调,当活动转换在它们生命周期的各种状态,如当一个活动被创建,停止,恢复,销毁。最重要的两个回调方法是:
onCreate()
You must implement this method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.
你必须实现这个方法,系统回调这个方法当创建你的活动的时候。在你实现期间,你应该初始化你的活动的基本必须的组件。最重要的是;你必须调用SetContentView()定义布局活动的用户界面。
onPause()
The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
系统调用这个方法作为第一个迹象表明用户离开你的活动(尽管它并不总意味着活动总被破坏)。这应该是你提交任何变化应坚持超越当前用户的会话(因为用户可能不回来了)
There are several other lifecycle callback methods that you should use in order to provide a fluid user experience between activities and handle unexpected interuptions that cause your activity to be stopped and even destroyed. All of the lifecycle callback methods are discussed later, in the section about Managing the Activity Lifecycle.
还有其他几个生命周期回调方法,你应该使用,以提供一个流体的用户体验活动和处理意外的中断以至于引起你的活动被停止和甚至被破坏。所有的生命周期回调方法进行讨论之后,在这一节关于管理生命周期的活动。
Implementing a user interface
The user interface for an activity is provided by a hierarchy of views—objects derived from the View class. Each view controls a particular rectangular space within the activity's window and can respond to user interaction. For example, a view might be a button that initiates an action when the user touches it.
用户界面为一个活动,是提供一个层次的views-objects源自视图类。每个视图控制一个特定的矩形空间在该活动的窗口,可以响应用户的交互。例如,一个视图可能是一个按钮,启动一个行动当用户触摸它。
Android provides a number of ready-made views that you can use to design and organize your layout. "Widgets" are views that provide a visual (and interactive) elements for the screen, such as a button, text field, checkbox, or just an image. "Layouts" are views derived from ViewGroup that provide a unique layout model for its child views, such as a linear layout, a grid layout, or relative layout. You can also subclass the View and ViewGroup classes (or existing subclasses) to create your own widgets and layouts and apply them to your activity layout.
Android提供了很多现成的视图,您可以使用它来设计和组织你的布局。“插件”是视图,这些视图提供视觉(和互动)屏幕元素,比如一个按钮,文本域,复选框,或者只是一个图像。“布局”是源自ViewGroup视图,提供一个独特的布局模式子视图,如线性布局,网格布局,或者相对的布局。你也可以细分视图和ViewGroup类(或现有的子类)来创建自己的小部件和布局,并将它们应用到你的activity布局中。
The most common way to define a layout using views is with an XML layout file saved in your application resources. This way, you can maintain the design of your user interface separately from the source code that defines the activity's behavior. You can set the layout as the UI for your activity with setContentView(), passing the resource ID for the layout. However, you can also create new Views in your activity code and build a view hierarchy by inserting new Views into a ViewGroup, then use that layout by passing the root ViewGroup to setContentView().
最常见的方式来定义一个布局使用视图是使用XML布局文件保存在您的应用程序资源。通过这种方式,您可以维护用户界面的设计,分别从源代码中,定义了活动的行为。你可以设置的布局为你的活动与UI 用setContentView(),通过对布局的资源ID。然而,你也可以在你的活动创建新的视图代码并构建一个视图层次插入新视图ViewGroup,然后用这个布局通过根ViewGroup到setContentView()。
For information about creating a user interface, see the User Interface documentation.
有关信息创建一个用户界面,看用户界面文档。
Declaring the activity in the manifest
You must declare your activity in the manifest file in order for it to be accessible to the system. To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:
你必须声明你的activity在manifest文件中以访问系统。声明你的activity,打开你的manifest文件并添加一个<activity>元素的子元素<application>。例如:
There are several other attributes that you can include in this element, to define properties such as the label for the activity, an icon for the activity, or a theme to style the activity's UI. The android:name attribute is the only required attribute—it specifies the class name of the activity. Once you publish your application, you should not change this name, because if you do, you might break some functionality, such as application shortcuts (read the blog post, Things That Cannot Change).
还有其他几个属性,您可以包括在这个元素,定义属性如标签活动,该活动的一个图标,或一个主题风格活动的UI。android:name属性是惟一需要attribute-it指定类名的活动。 一旦你发布你的应用程序,您不应该更改这个名称,因为如果你这样做了,你可能会破坏某些功能,比如应用程序快捷方式(读这篇博客文章,一些不能改变)。
See the <activity> element reference for more information about declaring your activity in the manifest
看到<activity>元素引用信息声明你的activity在清单中
Using intent filters
上图:The activity lifecycle.(重要)
主要七种方法:onCreate()
onStart()
onRestart()
onResume()
onPause()
onStop()
onDerstory()