Activity --译 (未完结)

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.

An application usually consists of multiple activities that areloosely 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" queue mechanism, so, when the user is done with the current activity and presses the BACK key, 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是android应用程序提供的一个用于跟用户(user)进行交互的组件,比如拨打电话、照相、发送一封邮件或者查看地图。每一个Activity就是一个窗口,你可以在里面绘制UI。它可以填充屏幕或者漂浮在其他的窗口之上。

通常一个android的应用程序由多个Activity组成,它们之间的相互联系并不是很紧密(that are loosely bound to each other)。 典型的应用就是,一个activity作为程序的主界面,程序一开始运行的时候用户(user)看到的就是这个界面。每个activity都可以启动其他的activity用于做不同的事情。每次新的activity启动时,先前的activity就会停止(stop),但是系统在堆中保存了这个activity。当有一个新的activity运行时,它被压到堆栈(back stack)中同时获得界面的焦点。因为堆的一个最根本的机制就是“后进先出”。所以,当用户(user)做完需要在当前activity做的事情之后并且按下了返回键时,当前的activity就从堆栈中取出(popped)而且被销毁(destroyed),先前的那个activity就会重新开始。(更详细的说明请参看文档 Tasks and Back Stack 。)

当一个activity因为一个新的activity的启动而停止的时候,activity的生命周期中的回调函数就会通知它的状态改变。因为activity状态的改变,activity就会有若干个回调响应,无论是系统创建这个activity,还是停止这个activity,抑或是重启、销毁这个activity。每个回调函数中你都可以执行一些特殊的工作以适应activity状态的改变。比如说,activity停止的时候,你应该释放这个activity的大多数对象(objects),例如,网络或者数据库的连接。当这个activity又重新启动时,你又能够获得先前释放的这些必需资源。这些状态的转换就是activity的生命周期。

文档的其他的部分说明了怎样使用一个activity,包括详细说明activity的生命周期的工作原理,以便你能够很好的管理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:

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.
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.

创建一个activity

想要创建一个activity,你必须创建一个子类继承Activity(或者是Activity的子类)。在你的子类中,你需要实现activity生命周期中状态转换时系统需要调用的回调方法,比如activity正在被创建,停止,重启启动,销毁这些状态的改变。

其中,两个最重要的方法就是:

onCreate()

你必须实现这个方法。这样你创建你的activity时系统才可以调用到这个方法。在你的实现的代码中,你应该初始化你activity中需要使用的最基本的组件。最重要的就是,你必须调用setContentView()这个方法去定义一个layout去装载这个activity的UI。

 onPause()

当用户离开将要离开当前的activity的时候系统就会调用这个方法(这并不总意味着这个activity将会销毁)。通常在这个方法中你要提交所有程序数据的改变,这比处理当前用户的会话更加重要(因为用户可能再也不会返回了)。

android提供了几个activity生命周期的回调方法,如果你想给用户提供一个流畅的操作体验,你应该使用这些方法处理activity的停止和销毁引起的意想不到(unexpected)的中断(interuptions)。将会在下面的Managing the Activity Lifecycle中详述activity生命周期中的所有回调方法。

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.

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.

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 newViews 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().

For information about creating a user interface, see the User Interface documentation.

实现一个UI

activity的UI提供了许多具有层级关系的视图(view)-由view类衍生出的对象。activity中的每个view都有一个独有的矩形空间,它能够实现跟用户的交互。举个例子说吧,如果一个控件(view)是一个button的话,当用户触碰它,它就会产生一个响应(action)。

Android提供了许多已经可以使用的视图,你可以用它们组织成你的界面布局(layout)。"Widgets"就是屏幕中最基本的可见并且可以交互的最基本的视图,比如一个button,textfield,checkbox, 或者仅仅是一张图片(image)。“Layouts”是从“ViewGroup”衍生出个一个视图(view),用来提供为子视图(child views)提供一个可以布局的载体,比如liner layout, grid layout, relative layout。你可以定义自己的控件或者布局,通过继承View或者ViewGroup类(或者其他这两个类的子类),你可以使用自己定义控件和布局去创建activity的布局。

最常用的方法就是通过定义一个XML资源文件来实现布局(layout)和视图(view)的创建。使用这种方法,你可以轻松维护你的UI,而不需要改动你的业务代码。你可以通过使用setContentView()方法传入布局文件的资源ID来设置你的UI资源文件。你也可以在activity的代码创建新的视图(view),也可以创建一个具有层级关系的视图组(ViewGroup),然后通过使用setContentView()方法传入根ViewGroup

关于创建UI的详细信息,请参看文档User Interface

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:

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

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).

See the <activity> element reference for more information about declaring your activity in the manifest.


在manifest文件中声明你的activity

如果你想你的activity能够被系统获得,你必须在mainfest文件中声明它。要声明你的activity,你需要加入<activity>标签作为<application>的子标签。例如:

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

还有很多其他的属性你可以包括在这个标签中,比如定义activity的label,使用的图片(icon),甚至是activity的UI主题样式( a theme to style)。android:name属性是必须的,它用来指定与activity关联的类名(class name)。一旦你发布了你的应用,最好就不要修改你这个名字了,因为修改了,可能破坏某些功能,比如程序的快捷键(请参看博客Things That Cannot Change)。


Using intent filters

An <activity> element can also specify various intent filters—using the <intent-filter> element—in order to declare how other application components may activate it.

When you create a new application using the Android SDK tools, the stub activity that's created for you automatically includes an intent filter that declares the activity responds to the "main" action and should be placed in the "launcher" category. The intent filter looks like this:

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).

If you intend for your application to be self-contained and not allow other applications to activate its activities, then you don't need any other intent filters. Only one activity should have the "main" action and "launcher" category, as in the previous example. Activities that you don't want to make available to other applications should have no intent filters and you can start them yourself using explicit intents (as discussed in the following section).

However, if you want your activity to respond to implicit intents that are delivered from other applications (and your own), then you must define additional intent filters for your activity. For each type of intent to which you want to respond, you must include an <intent-filter> that includes an <action> element and, optionally, a <category> element and/or a <data>element. These elements specify the type of intent to which your activity can respond.

For more information about how your activities can respond to intents, see the Intents and Intent Filters document.

使用intent filters

一个<activity>可以指定很多intent filters-为了声明其他的应用怎样才能激动这个activity,需要使用<intent-filter>标签。当你使用Android SDK创建一个新的应用时,根(the stub)activity会自动为你创建一个intent filter,其中声明这个activity会响应"main" action,它category则是"launcher",这两个标签应该同时使用。就像下面代码中那样

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<action>标签表明了这个activity是程序的"main"入口。<category>标签则表明这个activity会被列入到系统的程序启动器列表中(为了让用户启动这个activity)。

如果你只打算让你的程序内部访问这个activity,而不允许其他应用程序访问的话,则你不需要使用任何的intent filter。只需要一个activity包含了"main" action标签和"launcher" category标签。就像上面的例子那样。

明晚继续。。


先翻译这一小段,有时间接着翻译。你个支持是我坚持的动力。


view---笔者也不知道该翻译成什么,才能够让大家很好的理解。大家好好意会下吧!呵呵。。

root View Group---笔者认为应该是最底层的ViewGroup, 如果知道根目录这个概念的应该很容易理解root View Group

                                                                                                                                        

由于水平有限,翻译有错误是在所难免的还希望不吝指正,互相交流提高。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值