`

【翻译】(3)活动

 
阅读更多

【翻译】(3)活动

 

see

http://developer.android.com/guide/topics/fundamentals/activities.html

 

原文见

http://developer.android.com/guide/topics/fundamentals/activities.html

 

Activities

 

活动

 

 

---------------------

 

Quickview

 

快速概览

 

* An activity provides a user interface for a single screen in your application

 

* 一个活动提供在你的应用程序中单一屏幕使用的用户界面

 

* Activities can move into the background and then be resumed with their state restored

 

* 活动可以移动到后台然后随状态的恢复而继续执行

 

In this document

 

本文目录

 

* Creating an Activity 创建一个活动

* Implementing a user interface 实现一个用户界面

* Declaring the activity in the manifest 在清单中声明活动

* Starting an Activity 启动一个活动

* Starting an Activity for a Result 启动一个活动获取结果

* Shutting Down an Activity 关闭一个活动

* Managing the Activity Lifecycle 管理活动的生命周期

* Implementing the lifecycle callbacks 实现生命周期回调

* Saving activity state 保存活动状态

* Handling configuration changes 处理配置的改变

* Coordinating activities 协调活动

 

Key classes

 

关键类

 

Activity

 

See also

 

参考

 

* Hello World Tutorial “你好世界”教程

* Tasks and Back Stack 任务和后退堆栈

 

---------------------

 

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

 

本文剩余部分讨论如何构建和使用活动的基础,包括活动生命周期如何工作的完整讨论,使你可以合理地管理不同动作状态之间的迁移。

 

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.

 

一个活动的用户界面由带层次的视图提供——继承自View类的对象。每个视图控制活动窗口内的一个特定矩形空间并且可以响应用户交互。例如,一个视图可以是一个按钮,在用户触摸时触发一个动作。

 

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的视图,提供其子视图的唯一布局模型,诸如线性布局,表格布局,或相对布局。你还可以子类化View和ViewGroup类(或已有的子类)以创建你自己的小部件和布局,并且把它们应用到你的活动布局。

 

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布局文件定义布局。用这种办法,你可以维护你的用户界面的设计,与定义活动行为的源代码分离。你可以用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>元素作为<application>元素的子节点。例如:

 

---------------

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

 

你可以在这个元素内包含几个其它属性以定义一些属性,诸如活动的标签,活动的图标,或者风格化活动用户界面的主题。The android:name属性是唯一必需的属性——它指定活动的类名。一旦你发布你的应用程序,你不应改变这个名称,因为如果你这样做,你可能破坏某些功能,诸如应用程序快捷方式(请阅读发表的博客文章,不能改变的东西)。

 

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

 

见<activity>元素参考手册以获取更多关于在清单中声明你的活动的更多信息。

 

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.

 

一个<activity>元素还可以指定不同的意图过滤器——使用<intent-filter>元素——以便声明其它应用程序组件如何可以激活它。

 

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:

 

当你使用Android SDK工具创建新的应用程序,它会自动为你创建的插桩活动,其中包含一个意图过滤器,它声明响应main动作的活动,并且应该放置在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>

---------------

 

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

 

<action>元素指出这个活动是应用程序的主入口点。<category>元素指出这个活动应该列在系统的应用程序启动器内(以允许用户启动这个活动)。

 

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

 

如果你想让你的应用程序自包含并且不允许其它应用程序激活它的活动,那么你不需要任何其它意图过滤器。应该只有一个活动拥有main动作和launcher分类,正如前面的例子。不想让其它应用程序使用的活动不应该拥有意图过滤器,你可以使用显式的意图自己启动它们(正如下面的章节讨论的)。

 

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.

 

然而,如果你想让你的活动响应其它应用程序传递的隐式意图(以及你自己),那么你必须为你的活动定义额外的意图过滤器。对于你想响应的每种意图,你必须包含一个<intent-filter>,它包含一个<action>元素,以及一个可选的<category>元素和/或一个<data>元素。这些元素指定你的活动可以响应的意图类型。

 

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

 

参考意图和意图过滤器文档,以获取关于你的活动如何可以响应意图的更多信息。

 

Starting an Activity

 

启动活动

 

You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start. The intent specifies either the exact activity you want to start or describes the type of action you want to perform (and the system selects the appropriate activity for you, which can even be from a different application). An intent can also carry small amounts of data to be used by the activity that is started.

 

你可以通过调用startActivity()启动另一个活动,传给它一个意图,描述你想启动的活动。意图指定你想启动的具体活动或描述你想执行的动作类型(而系统会为你选择合适的活动,甚至可以是来自一个不同的应用程序)。一个意图还可以携带被启动的活动使用的小量数据。

 

When working within your own application, you'll often need to simply launch a known activity. You can do so by creating an intent that explicitly defines the activity you want to start, using the class name. For example, here's how one activity starts another activity named SignInActivity:

 

当工作在你自己的应用程序中时,你将常常需要简单地启动一个已知活动。你可以通过创建一个意图,使用类名显式定义你想启动的活动,来做到这一点。例如,这里是一个活动如何启动另一个名为SignInActivity的活动。

 

--------------

Intent intent = new Intent(this, SignInActivity.class);

startActivity(intent);

--------------

 

However, your application might also want to perform some action, such as send an email, text message, or status update, using data from your activity. In this case, your application might not have its own activities to perform such actions, so you can instead leverage the activities provided by other applications on the device, which can perform the actions for you. This is where intents are really valuable—you can create an intent that describes an action you want to perform and the system launches the appropriate activity from another application. If there are multiple activities that can handle the intent, then the user can select which one to use. For example, if you want to allow the user to send an email message, you can create the following intent:

 

然而,你的应用程序还可能想执行一些动作,诸如发送电子邮件,文本消息,或状态更新,使用来自你的活动的数据。这种情况下,你的应用程序可能没有它自己的活动来执行这样的动作,所以取而代之,你可以借用设备上其它应用程序提供的活动,它们可以为你执行这些动作。这里意图真正显示其价值——你可以创建描述你想执行动作的意图,而系统从另一个应用程序启动合适的活动。如果有多个可以处理意图的活动,那么用户可以选择使用哪个。例如,如果你想允许用户发送电子邮件消息,你可以创建以下意图:

 

--------------

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);

startActivity(intent);

--------------

 

The EXTRA_EMAIL extra added to the intent is a string array of email addresses to which the email should be sent. When an email application responds to this intent, it reads the string array provided in the extra and places them in the "to" field of the email composition form. In this situation, the email application's activity starts and when the user is done, your activity resumes.

 

额外添加到意图的EXTRA_EMAIL是该邮件应该被发送到的邮箱地址的字符串数组。当一个电子邮件应用程序响应这个意图,它读取额外提供的字符串数组,并且把它们放进电子邮件撰写表单的to域(注:收件人)。在这种情况下,电子邮件应用程序的活动被启动,并且当用户完成后,你的活动恢复执行。

 

Starting an activity for a result

 

启动活动获取结果

 

Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult() (instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult() callback method. When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.

 

有时,你可能想从你启动的活动中接收结果。在那种情况下,通过调用startActivityForResult()启动活动(而非startActivity())。然后为了接收来自随后活动的结果而实现onActivityResult()回调方法。当随后活动完成时,传递给onActivityResult()方法的Intent中会返回结果。

 

For example, perhaps you want the user to pick one of their contacts, so your activity can do something with the information in that contact. Here's how you can create such an intent and handle the result:

 

例如,你可能想让用户选取他们的电话簿(注:联系人)的一项,使你的活动可以处理那个电话簿的信息。这里是你可以如何创建这样的意图来处理结果:

 

-----------------------

 

private void pickContact() {

    // Create an intent to "pick" a contact, as defined by the content provider URI

    // 创建一个意图去“选择”电话簿,正如内容提供者URI所定义的那样

    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);

    startActivityForResult(intent, PICK_CONTACT_REQUEST);

}

 

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST

    // 如果请求成功(OK)并且请求是PICK_CONTACT_REQUEST

    if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {

        // Perform a query to the contact's content provider for the contact's name

        // 执行到电话簿的内容提供者的查询以获取电话簿的名字

        Cursor cursor = getContentResolver().query(data.getData(),

        new String[] {Contacts.DISPLAY_NAME}, null, null, null);

        if (cursor.moveToFirst()) { // True if the cursor is not empty 真,如果游标非空

            int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

            String name = cursor.getString(columnIndex);

            // Do something with the selected contact's name...

            // 处理选中的电话簿名字...

        }

    }

}

 

-----------------------

 

This example shows the basic logic you should use in your onActivityResult() method in order to handle an activity result. The first condition checks whether the request was successful—if it was, then the resultCode will be RESULT_OK—and whether the request to which this result is responding is known—in this case, the requestCode matches the second parameter sent with startActivityForResult(). From there, the code handles the activity result by querying the data returned in an Intent (the data parameter).

 

这个例子展示你应该在你的onActivityResult()方法内使用的基本逻辑,以便于处理活动的结果。第一个条件检查请求是否成功——如果成功,那么resultCode将是RESULT_OK——以及响应这个结果的请求是否已知——在这种情况下,requestCode匹配用startActivityForResult()发送的第二个参数。从那里开始,代码通过查询意图返回的数据处理活动结果(data参数)。

 

What happens is, a ContentResolver performs a query against a content provider, which returns a Cursor that allows the queried data to be read. For more information, see the Content Providers document.

 

发生的事情是,一个ContentResolver执行对内容提供者的查询,它返回一个Cursor,允许读取被查询的数据。请参考内容提供者文档获取更多信息。

 

For more information about using intents, see the Intents and Intent Filters document.

 

请参考意图和意图过滤器文档以获取更多关于使用意图的信息。

 

Shutting Down an Activity

 

关闭一个活动

 

You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity().

 

你可以通过调用它的finish()方法关闭一个活动。你还可以关闭一个你在之前通过调用finishActivity()启动的独立活动。

 

Note: In most cases, you should not explicitly finish an activity using these methods. As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you, so you do not need to finish your own activities. Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity.

 

注意:在大部分情况下,你不应该用这些方法显式地结束一个活动。正如下面关于活动生命周期的章节中讨论的,Android系统为你管理一个活动的生命周期,所以你不需要结束你自己的活动。调用这些方法可能不利地影响期待的用户体验,应该只用于你绝不愿意让用户返回到该活动实例的时候。

 

Managing the Activity Lifecycle

 

管理活动生命周期

 

Managing the lifecycle of your activities by implementing callback methods is crucial to developing a strong and flexible application. The lifecycle of an activity is directly affected by its association with other activities, its task and back stack.

 

通过实现回调方法管理你的活动的生命周期,对于开发强壮和灵活的应用程序是非常重要的。活动的生命周期直接受其它活动,它的任务,以及后退堆栈,对其的关联所影响。

 

An activity can exist in essentially three states:

 

本质上,一个活动可以存在于三种状态

 

Resumed

 

被恢复

 

The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)

 

活动在屏幕前台,拥有用户焦点。(这种状态有时还被称为“运行中”)

 

Paused

 

被暂停

 

Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.

 

另一个活动在前台获取焦点,但原来的活动仍然可视。就是说,另一个活动在这个活动的上方可见,而那个活动部分透明或没有覆盖整个屏幕。一个被暂停的活动完全是生存的(Activity对象被保留在内存中,它维护整个状态和成员变量信息,而且仍然依附在窗口管理器),但它在极低内存的情形下会被系统杀死。

 

Stopped

 

被停止

 

The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.

 

活动完全被另一个活动遮盖(活动现在在“后台”)。一个被停止的活动仍然生存(Activity对象被保留在内存,它维护所有状态和成员变量信息,但不依附在窗口管理器)。然而,它对用户不再可视,而且在其它地方需要内存时它可能会被系统杀死。

 

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.

 

如果一个动作被暂停或停止,系统可以通过叫它结束(调用它的finish()方法),从内存中删除它,或简单地杀死它的进程。当活动再次被打开(在被结束或杀死之后),它必须整个被创建。

 

Implementing the lifecycle callbacks

 

实现生命周期回调

 

When an activity transitions into and out of the different states described above, it is notified through various callback methods. All of the callback methods are hooks that you can override to do appropriate work when the state of your activity changes. The following skeleton activity includes each of the fundamental lifecycle methods:

 

当一个活动迁移进出于前面描述的不同状态时,它通过不同的回调方法被通知。所有回调方法都是钩子,你可以覆盖以便在你的活动状态改变时完成合适的工作。以下支架活动包含每个基础的生命周期方法:

 

--------------------

 

public class ExampleActivity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // The activity is being created.

        // 活动正在被创建。

    }

    @Override

    protected void onStart() {

        super.onStart();

        // The activity is about to become visible.

        // 活动将变得可见

    }

    @Override

    protected void onResume() {

        super.onResume();

        // The activity has become visible (it is now "resumed").

        // 活动变得可见(现在它被恢复)。

    }

    @Override

    protected void onPause() {

        super.onPause();

        // Another activity is taking focus (this activity is about to be "paused").

        // 另一个活动获取焦点(这个活动将被暂停)。

    }

    @Override

    protected void onStop() {

        super.onStop();

        // The activity is no longer visible (it is now "stopped")

        // 活动不再可见(现在被停止)

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        // The activity is about to be destroyed.

        // 活动将被销毁

    }

}

 

--------------------

 

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above.

 

注意:你对这些生命周期方法的实现必须总是在进行任何工作之前调用超类的实现,就像上面的例子那样。

 

Taken together, these methods define the entire lifecycle of an activity. By implementing these methods, you can monitor three nested loops in the activity lifecycle:

 

总体看,这些方法定义活动的整个生命周期。通过实现这些方法,你可以监视活动生命周期内的三个嵌套循环:

 

* The entire lifetime of an activity happens between the call to onCreate() and the call to onDestroy(). Your activity should perform setup of "global" state (such as defining layout) in onCreate(), and release all remaining resources in onDestroy(). For example, if your activity has a thread running in the background to download data from the network, it might create that thread in onCreate() and then stop the thread in onDestroy().

 

* 一个活动的整个生命期发生在onCreate()的调用和onDestroy()的调用之间。你的生命周期应该在onCreate()内执行“全局”状态的配置,在onDestroy()内执行所有剩余资源的释放。例如,如果你的活动有一个运行在后台的线程通过网络下载数据,它可能在onCreate()内创建线程然后在onDestroy()内停止该线程。

 

* The visible lifetime of an activity happens between the call to onStart() and the call to onStop(). During this time, the user can see the activity on-screen and interact with it. For example, onStop() is called when a new activity starts and this one is no longer visible. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor changes that impact your UI, and unregister it in onStop() when the user can no longer see what you are displaying. The system might call onStart() and onStop() multiple times during the entire lifetime of the activity, as the activity alternates between being visible and hidden to the user.

 

* 一个活动可见的生命期发生在onStart()的调用和onStop()的调用之间。在此期间,用户可以在屏幕上看到活动并与之交互。例如,当一个新的活动启动时而原来的不再可见时,onStop()被调用。在这两个方法之间,你可以维护需要展示活动给用户看的资源。例如,你可以在onStart()内注册一个BroadcastReceiver以监视影响你的用户界面的改变,并且当用户可能不再看到你要显示的东西时在onStop()内注销它们。当活动在对用户可见和隐藏之间相互切换时,系统可能在活动的整个生命期多次调用onStart()和onStop()。

 

* The foreground lifetime of an activity happens between the call to onResume() and the call to onPause(). During this time, the activity is in front of all other activities on screen and has user input focus. An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.

 

* 一个活动的前台生命期发生在onResume()的调用和onPause()调用之间。在此期间,活动在屏幕上所有其它活动的前面,并且拥有输入焦点。一个活动可以频繁地迁移进出前台——例如,onPause()在设备进入休眠时或者当对话框(注:这里可能指来电?)出现时被调用。因为这个状态可能经常迁移,在这两个方法内的代码应该非常轻量级以避免导致用户等待的缓慢过渡。

 

Figure 1 illustrates these loops and the paths an activity might take between states. The rectangles represent the callback methods you can implement to perform operations when the activity transitions between states.

 

图1展示一个活动可能在状态之间途经的这些循环和路径。矩形表示你可以实现的回调方法,以便于在活动在状态之间迁移时执行某些操作。

 

-----------------------

 

(图略:

中间

1. 活动被启动

2. onCreate()

3. onStart()

4. onResume()

5. 活动正在运行

5->6 另一个活动来到前台中

6. onPause()

6->7 活动不再可见

7. onStop()

7->8 活动正在完成或正在被系统销毁

8. onDestroy()

9. 活动关闭

 

左侧

6->10, 7->10 高优先级的应用需要内存

10. 应用进程被销毁

10->2 用户导航到活动

 

右侧

7->11 用户导航到活动

11. onRestart()

11->3

6->4 用户返回到活动

 

Figure 1. The activity lifecycle.

 

图1. 活动生命周期。

 

-----------------------

 

The same lifecycle callback methods are listed in table 1, which describes each of the callback methods in more detail and locates each one within the activity's overall lifecycle, including whether the system can kill the activity after the callback method completes.

 

相同的生命周期方法被列举在表1中,更详细地描述每个回调方法以及在活动的整体生命周期中定位每个方法,包括系统是否可以在回调方法完成之后杀死活动。

 

-----------------------

 

Table 1. A summary of the activity lifecycle's callback methods.

 

表1. 活动声明周期回调方法的总结。

 

* Method 方法

* Description 描述

* Killable after? 活动在此之后是否可以被杀死

* Next 下一个回调

 

1. onCreate()

 

Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later).

 

当活动被首次创建时被调用。这里你应该完成你的所有正常的静态设置——创建视图,绑定数据到列表,等等。这个方法传递一个Bundle对象,包含活动的前一次状态,如果那个状态被快照了(见后面的保存活动状态)。

 

Always followed by onStart().

 

总是在后面执行onStart()

 

* No 否

* onStart()

 

2. onRestart()

 

Called after the activity has been stopped, just prior to it being started again.

 

在活动被停止后调用,正好在它被再次启动之前。

 

Always followed by onStart()

 

总是在后面执行onStart()

 

* No 否

* onStart()

 

3. onStart()

 

Called just before the activity becomes visible to the user.

 

正好在活动变得对用户可见之前被调用。

 

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

 

如果活动移到前台则后面执行onResume(),或者如果它隐藏了则执行onStop()。

 

* No 否

* onResume() or onStop()

 

4. onResume()

 

Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

 

正好在活动开始与用户交互之前被调用。此时活动在活动堆栈的顶部,用户的输入会传给它。

 

Always followed by onPause().

 

总是在后面执行onPause()。

 

* No 否

* onPause()

 

5. onPause()

 

Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

 

当系统准备开始继续执行另一个活动时被调用。这个方法通常用于提交对持久数据的未保存修改,停止动画和其它可能消耗CPU的事情,等等。它应该非常快地完成工作,因为下一个活动将不会被恢复,直至它返回。

 

Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

 

如果活动回到前方,后面执行onResume(),或者如果它变得对用户不可见,则后面执行onStop()。

 

* Yes 是

* onResume() or onStop()

 

6. onStop()

 

Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

 

当活动对用户不再可见时被调用。它的发生可能是因为活动被销毁,或者因为另一个活动(现有的或新的活动)已经被恢复并且遮住它。

 

Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

 

后面执行onRestart(),如果活动返回与用户交互,或者执行onDestroy(),如果这个活动死去。

 

* Yes 是

* onRestart() or onDestroy()

 

7. onDestroy()

 

Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

 

在活动销毁前被调用。这是活动将接收到的最后调用。它被调用是因为活动完成(有人在此处调用finish()),或者因为系统暂时销毁这个活动的实例以节约空间。你可以用isFinishing()方法区分这两种场景。

 

* Yes 是

* nothing 无

 

--------------------------

 

The column labeled "Killable after?" indicates whether or not the system can kill the process hosting the activity at any time after the method returns, without executing another line of the activity's code. Three methods are marked "yes": (onPause(), onStop(), and onDestroy()). Because onPause() is the first of the three, once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage. However, you should be selective about what information must be retained during onPause(), because any blocking procedures in this method block the transition to the next activity and slow the user experience.

 

标有“之后可被杀死”的列指出在该方法返回后的任意时刻,系统是否可以杀死持有活动的进程,而不会执行活动代码的其它代码行。有三个方法被标注为是:(onPause(),onStop(),以及onDestroy())。因为onPause()是这三个方法中的第一个,一旦活动被创建,onPause()是保证进程可能被杀死前调用的最后一个方法——如果系统必须紧急回收内存的话,之后,onStop()和onDestroy()可能不被调用。因此,你应该使用onPause()把关键的持久化数据(诸如用户编辑)写到存储器中。因此,你应该考虑哪些信息必须在onPause()期间保存,因为这个方法的任何阻塞过程将阻塞到下一个活动的迁移,减慢用户体验。

 

Methods that are marked "No" in the Killable column protect the process hosting the activity from being killed from the moment they are called. Thus, an activity is killable from the time onPause() returns to the time onResume() is called. It will not again be killable until onPause() is again called and returns.

 

在可杀死列中标注为否的方法保护持有活动的进程在它们被调用的时候不会被杀死。因此,活动从onPause()时刻返回到onResume()被调用时刻之间是可被杀死的。它将不再可以被杀死,直至onPause()再次被调用和返回。

 

Note: An activity that's not technically "killable" by this definition in table 1 might still be killed by the system—but that would happen only in extreme circumstances when there is no other recourse. When an activity might be killed is discussed more in the Processes and Threading document.

 

注意:在表1定义的技术上不是“可杀死”的活动仍然可能被系统杀死——仅当没有其它资源的极端情况出现时。在进程和线程文档中会更详细地讨论活动何时可能被杀死。

 

Saving activity state

 

保存活动状态

 

The introduction to Managing the Activity Lifecycle briefly mentions that when an activity is paused or stopped, the state of the activity is retained. This is true because the Activity object is still held in memory when it is paused or stopped—all information about its members and current state is still alive. Thus, any changes the user made within the activity are retained so that when the activity returns to the foreground (when it "resumes"), those changes are still there.

 

对管理活动生命周期的介绍中简单地提及了,当活动被暂停或停止时,活动状态被记住。这是对的,因为Activity对象仍然被持有在内存中,当它被暂停或停止——所有关于它的成员函数和当前状态的信息仍然生存。这样,用户在活动内作出的任何改变被维持,致使当活动回到前台(当它“恢复”时),那些改变仍然在那里。

 

However, when the system destroys an activity in order to recover memory, the Activity object is destroyed, so the system cannot simply resume it with its state intact. Instead, the system must recreate the Activity object if the user navigates back to it. Yet, the user is unaware that the system destroyed the activity and recreated it and, thus, probably expects the activity to be exactly as it was. In this situation, you can ensure that important information about the activity state is preserved by implementing an additional callback method that allows you to save information about the state of your activity: onSaveInstanceState().

 

然而,当系统销毁活动以恢复内存时,Activity对象会被销毁,所以系统不能简单地用完整的状态恢复它。取而代之,系统必须重新创建活动对象,如果用户导航回到它。然而,用户不会意识到系统销毁活动和重新创建它,因此,他们可能期待活动准确地保持原状。在这种情形下,你可以通过实现一个额外的回调函数,确保关于活动状态的重要信息被保留,以允许你保存关于你的活动的状态:onSaveInstanceState()。

 

The system calls onSaveInstanceState() before making the activity vulnerable to destruction. The system passes this method a Bundle in which you can save state information about the activity as name-value pairs, using methods such as putString() and putInt(). Then, if the system kills your application process and the user navigates back to your activity, the system recreates the activity and passes the Bundle to both onCreate() and onRestoreInstanceState(). Using either of these methods, you can extract your saved state from the Bundle and restore the activity state. If there is no state information to restore, then the Bundle passed to you is null (which is the case when the activity is created for the first time).

 

系统在让活动变脆弱以析构之前调用onSaveInstanceState()。系统传递此方法一个Bundle,你可以把关于活动的状态信息作为名称-值对保存进它,使用方法诸如putString()和putInt()。然而,如果系统杀死你的应用程序进程而用户导航回到你的活动,系统会重新创建活动并传递该Bundle给onCreate()和onRestoreInstanceState()。使用这两个方法任意一个,你可以从Bundle中抽取出你的被保存状态并恢复活动状态。如果没有要恢复的状态信息,那么传给你的Bundle为null(这种情况发生在活动被首次创建时)。

 

--------------------------

 

(图略:

1. 活动正在运行

1->2 另一个活动进来到前台

2. onSaveInstanceState() 保存你的活动状态

3. 活动不可见

--(左)

3->4 带高优先级的应用需要内存

4. 应用进程被杀死 *

* 活动实例被销毁,但来自onSaveInstanceState()的状态被保存

4->5 用户导航到该活动

5. onCreate()或onRestoreInstanceState() 恢复你的活动状态

5->1

--(右)

3-><4> 用户导航到活动

<4> onRestart()

<4>->1 活动实例是完好的;不需要恢复状态

 

Figure 2. The two ways in which an activity returns to user focus with its state intact: either the activity is destroyed, then recreated and the activity must restore the previously saved state, or the activity is stopped, then resumed and the activity state remains intact.

 

图2. 一个活动用它完好的状态返回到用户焦点的两种方式:要么活动被销毁,然后被重新创建而活动必须恢复之前被保存的状态,要么活动被停止,然后被恢复而活动状态保持完好。

 

--------------------------

 

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity). If the system calls onSaveInstanceState(), it does so before onStop() and possibly before onPause().

 

注意:不保证onSaveInstanceState()在你的活动销毁前调用,因为有些情况下,你没必要保存状态(诸如用户使用后退按钮离开活动,因为用户正在显式地关闭活动)。如果系统调用onSaveInstanceState(),它在onStop()和可能在onPause()之前调用它。

 

--------------------------

 

However, even if you do nothing and do not implement onSaveInstanceState(), some of the activity state is restored by the Activity class's default implementation of onSaveInstanceState(). Specifically, the default implementation calls the corresponding onSaveInstanceState() method for every View in the layout, which allows each view to provide information about itself that should be saved. Almost every widget in the Android framework implements this method as appropriate, such that any visible changes to the UI are automatically saved and restored when your activity is recreated. For example, the EditText widget saves any text entered by the user and the CheckBox widget saves whether it's checked or not. The only work required by you is to provide a unique ID (with the android:id attribute) for each widget you want to save its state. If a widget does not have an ID, then the system cannot save its state.

 

然而,即便你不做任何事情,不实现onSaveInstanceState(),某些活动状态也会被onSaveInstanceState()的Activity类默认实现所恢复。特别地,默认实现对布局内的每个视图调用相应的onSaveInstanceState(),允许每个视图提供应该被保存的关于它自身的信息。Android框架内几乎每个小部件都会合理地实现这个方法,致使对用户界面的任何可见变化在你的活动被重新创建时被自动保存和恢复。例如,EditText小部件保存用户输入的任意文本,而CheckBox小部件保存它是否被勾选。你唯一需要的工作是向每个你想保存其状态的小部件提供唯一ID(使用android:id属性)。如果小部件没有ID,那么系统将不能保存它的状态。

 

------------------

 

You can also explicitly stop a view in your layout from saving its state by setting the android:saveEnabled attribute to "false" or by calling the setSaveEnabled() method. Usually, you should not disable this, but you might if you want to restore the state of the activity UI differently.

 

你还可以通过设置android:saveEnabled的属性为false或通过调用setSaveEnabled()方法,显式地阻止你的布局中的一个视图保存其状态。通常,你不应该关闭它,但如果你想有区别地恢复活动用户界面的状态,你可以这样做。

 

------------------

 

Although the default implementation of onSaveInstanceState() saves useful information about your activity's UI, you still might need to override it to save additional information. For example, you might need to save member values that changed during the activity's life (which might correlate to values restored in the UI, but the members that hold those UI values are not restored, by default).

 

虽然onSaveInstanceState()的默认实现保存关于你的活动的用户界面的有用信息,但你仍然可能需要覆盖它以保存额外数据。例如,你可能需要保存在活动生存期被改变的数值(它可能与在用户界面中被恢复的值有关,但持有那些用户界面的值的成员变量默认是不会被恢复的)。

 

Because the default implementation of onSaveInstanceState() helps save the state of the UI, if you override the method in order to save additional state information, you should always call the superclass implementation of onSaveInstanceState() before doing any work. Likewise, you should also call the supercall implementation of onRestoreInstanceState() if you override it, so the default implementation can restore view states.

 

因为onSaveInstanceState()的默认实现有助于保存用户界面的数据,如果你覆盖此方法以保存额外的状态信息,你应该在做任何工作前总是调用onSaveInstanceState()的超类实现。同样地,你还应该调用onRestoreInstanceState()的超类调用实现,如果你覆盖它,致使默认实现可以恢复视图状态。

 

Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

 

注意:因为onSaveInstanceState()不保证被调用,你应该只使用它记录活动的瞬态状态(用户界面的状态)——你从不应该使用它保存持久数据。取而代之的是,你应该在用户离开活动时,使用onPause()保存持久数据(诸如应该被保存到数据库内的数据)。

 

A good way to test your application's ability to restore its state is to simply rotate the device so that the screen orientation changes. When the screen orientation changes, the system destroys and recreates the activity in order to apply alternative resources that might be available for the new screen configuration. For this reason alone, it's very important that your activity completely restores its state when it is recreated, because users regularly rotate the screen while using applications.

 

测试你的应用程序恢复状态的能力的好方法是,简单地旋转设备致使屏幕方向改变。当屏幕的方向改变时,系统销毁并重新创建活动以应用对于新的屏幕配置可用的可选资源。只有因为这个原因,在重新创建时,你的应用需要完全恢复其状态是很重要的,因为用户在使用应用程序时经常旋转屏幕。

 

Handling configuration changes

 

处理配置改变

 

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android recreates the running activity (the system calls onDestroy(), then immediately calls onCreate()). This behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that you've provided (such as different layouts for different screen orientations and sizes).

 

一些设备配置可以在运行时期间改变(诸如屏幕方向,键盘可用性,和语言)。当这么一种改变发生时,Android重新创建正在运行中的活动(系统调用onDestroy(),然后立刻调用onCreate())。此行为被设计为有助于你的应用程序通过用你提供的可选资源(诸如用于不同屏幕方向和大小的不同布局)自动重新加载你的应用程序来适应新的配置。

 

If you properly design your activity to handle a restart due to a screen orientation change and restore the activity state as described above, your application will be more resilient to other unexpected events in the activity lifecycle.

 

如果你正确地设计你的活动以处理由于屏幕方向改变而导致的重启,并且如上面描述的那样恢复活动状态,那么你的应用程序将对活动生命周期的其它非预期事件更有恢复能力(注:有弹性)。

 

The best way to handle such a restart is to save and restore the state of your activity using onSaveInstanceState() and onRestoreInstanceState() (or onCreate()), as discussed in the previous section.

 

处理这样的重启的最佳方法是使用onSaveInstanceState()和onRestoreInstanceState()(或onCreate())来保存和恢复你的活动状态,正如之前章节讨论的那样。

 

For more information about configuration changes that happen at runtime and how you can handle them, read the guide to Handling Runtime Changes.

 

想获得关于在运行期发生的配置改变以及你如何可以处理它们的更多信息,请阅读处理运行时改变的指引。

 

Coordinating activities

 

协调活动

 

When one activity starts another, they both experience lifecycle transitions. The first activity pauses and stops (though, it won't stop if it's still visible in the background), while the other activity is created. In case these activities share data saved to disc or elsewhere, it's important to understand that the first activity is not completely stopped before the second one is created. Rather, the process of starting the second one overlaps with the process of stopping the first one.

 

当一个活动启动另一个,这两个活动经历生命周期的迁移。当另一个活动被创建时,第一个活动暂停并且停止(虽然它可能不会停止,如果它仍然在后台可见的话)。一旦这些活动共享保存到磁盘或其它地方的数据,了解第一个活动不会在第二个活动创建之前完全被停止,是尤为重要的。相反,启动第二个活动的进程与停止第一个活动的进程有重叠(注:应该是指时间上的重叠)。

 

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:

 

特别地,当两个活动在同一个进程并且一个活动启动另一个时,一个声明周期回调次序被良好定义。这里,在活动A启动活动B时发生的操作次序:

 

1. Activity A's onPause() method executes.

 

1. 执行活动A的onPause()方法

 

2. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)

 

2. 依次执行活动B的onCreate()、onStart()以及onResume()方法。(活动B现在拥有用户焦点。)

 

3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

 

3. 然后,如果活动A不再在屏幕上可见,那么它的onStop()方法会执行。

 

This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another. For example, if you must write to a database when the first activity stops so that the following activity can read it, then you should write to the database during onPause() instead of during onStop().

 

这个可预计的生命周期回调序列允许你管理从一个活动到另一个活动的信息迁移。例如,如果你必须在第一次活动停止时写入数据库以便允许活动可以读取它,那么你应该在onPause()期间写入数据库,而非在onStop()之间。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 3.2 r1 - 03 Oct 2011 18:28

 

Android 4.0 r1 - 08 Mar 2012 0:34

 

-------------------------------

 

patch:

1. Android 4.0 r1 - 08 Mar 2012 0:34

(1)

++Shutting Down an Activity

 

(2)

the basic "last in, first out" queue mechanism,

->

the basic "last in, first out" stack mechanism,

 

(3)

current activity and presses the BACK key,

->

current activity and presses the Back button,

 

(4)

are retained in memory, so that

->

are retained so that

 

(5)

--Figure 2. The two ways in which an activity returns to user focus with its state intact: either the activity is stopped, then resumed and the activity state remains intact (left), or the activity is destroyed, then recreated and the activity must restore the previous activity state (right).

 

(6)

save information about the state of your activity and then restore it when the the system recreates the activity.

->

save information about the state of your activity: onSaveInstanceState().

 

(7)

The callback method in which you can save information about the current state of your activity is onSaveInstanceState(). The system calls this method before making the activity vulnerable to being destroyed and passes it a Bundle object. The Bundle is where you can store state information about the activity as name-value pairs, using methods such as putString(). Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() so you can restore the activity state you saved during onSaveInstanceState(). If there is no state information to restore, then the Bundle passed to onCreate() is null.

->

The system calls onSaveInstanceState() before making the activity vulnerable to destruction. The system passes this method a Bundle in which you can save state information about the activity as name-value pairs, using methods such as putString() and putInt(). Then, if the system kills your application process and the user navigates back to your activity, the system recreates the activity and passes the Bundle to both onCreate() and onRestoreInstanceState(). Using either of these methods, you can extract your saved state from the Bundle and restore the activity state. If there is no state information to restore, then the Bundle passed to you is null (which is the case when the activity is created for the first time).

 

(8)

++Figure 2. The two ways in which an activity returns to user focus with its state intact: either the activity is destroyed, then recreated and the activity must restore the previously saved state, or the activity is stopped, then resumed and the activity state remains intact.

 

(9)

when the user leaves your activity using the BACK key,

->

when the user leaves your activity using the Back button,

 

(10)

If the method is called, it is always called before onStop() and possibly before onPause().

->

If the system calls onSaveInstanceState(), it does so before onStop() and possibly before onPause().

 

(11)

the default implementation calls onSaveInstanceState() for every View in the layout,

->

the default implementation calls the corresponding onSaveInstanceState() method for every View in the layout,

 

(12)

If a widget does not have an ID, then it cannot save its state.

->

If a widget does not have an ID, then the system cannot save its state.

 

(13)

onSaveInstanceState() before doing any work.

->

onSaveInstanceState() before doing any work. Likewise, you should also call the supercall implementation of onRestoreInstanceState() if you override it, so the default implementation can restore view states.

 

(14)

that might be available for the new orientation.

->

that might be available for the new screen configuration.

 

(15)

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed immediately by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that you've provided. If you design your activity to properly handle this event, it will be more resilient to unexpected events in the activity lifecycle.

->

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android recreates the running activity (the system calls onDestroy(), then immediately calls onCreate()). This behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that you've provided (such as different layouts for different screen orientations and sizes).

 

(16)

++If you properly design your activity to handle a restart due to a screen orientation change and restore the activity state as described above, your application will be more resilient to other unexpected events in the activity lifecycle.

 

(17)

The best way to handle a configuration change, such as a change in the screen orientation, is to simply preserve the state of your application using onSaveInstanceState() and onRestoreInstanceState() (or onCreate()), as discussed in the previous section.

->

The best way to handle such a restart is to save and restore the state of your activity using onSaveInstanceState() and onRestoreInstanceState() (or onCreate()), as discussed in the previous section.

 

(18)

For a detailed discussion about configuration changes that happen at runtime and how you should handle them, read Handling Runtime Changes.

->

For more information about configuration changes that happen at runtime and how you can handle them, read the guide to Handling Runtime Changes.

 

(19)

两张图都改过

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证中描述的条款进行修改)


分享到:
评论

相关推荐

    全新版大学英语综合教程3课文原文及翻译.doc

    ### 3. 云计算与存储 - **云存储解决方案**:考虑到这是一份电子文档,可能会被上传到云端进行共享和备份。云存储提供了便捷的数据访问方式,并且可以支持多人协作编辑文档。 - **在线文档协作**:文档的编写和修改...

    新视野大学英语3翻译答案

    【新视野大学英语3翻译答案】的解析及学习要点: 单元1: 1. 这句话表达了对困境中依然支持自己的人的感激之情。学习点:掌握“stick by”表示“在困难时期支持”的用法。 2. 句子描述了某人因出版畅销书而经济状况...

    “Legged robots that balance“翻译活动计划及人员安排1

    本文主要介绍了名为"“Legged robots that balance”"的翻译活动计划及其人员安排,目标是将一本关于四足机器人的英文书籍翻译成中文。该活动通过QQ群组织多人协作完成,参与者需具备一定的专业知识和英语能力。 1....

    功能翻译翻译目的论简介PPT课件.pptx

    译文的交际目的决定了整个翻译活动的方向。 2. **连贯准则**:译文需考虑接收者的背景知识和实际需求,确保语义连贯,使译文读者能理解和接受,遵循译入语的表达习惯。 3. **忠实准则**:原文和译文之间存在一定...

    一款翻译软件源码

    3. UI设计:查看布局文件以了解Android UI组件的使用,如EditText(文本输入)、TextView(显示结果)等。 4. 用户交互:了解如何处理按钮点击事件,将用户输入发送到翻译API,并显示返回的结果。 5. 异步处理:由于...

    UiPath Level3 资料翻译

    Level3在UiPath的学习路径中通常代表进阶水平,涉及更复杂的活动和概念。 这份“UiPath Level3 资料翻译”包含的是对原英文学习资料的中文翻译,目的是帮助那些在学习过程中遇到语言障碍的人更好地理解内容。尽管...

    OSTC2015-CSDN翻译社区介绍

    社区的主要活动包括将英文技术文档翻译成中文,涉及的技术资料涵盖了各种技术指南、手册、学术论文以及标准规范等。 #### 翻译内容及版权类型 CSDN CODE翻译社区主要翻译的内容为英文技术文档,其中包括技术资料、...

    机器翻译 ACL 2012 论文集

    《机器翻译ACL 2012论文集》是人工智能领域中一次重要的学术交流活动——国际计算语言学协会(Association for Computational Linguistics, ACL)在2012年举办的关于机器翻译研究的论文合集。这一会议聚集了全球顶尖...

    新视野 大学英语 课文翻译3

    3. **社会认同与个人形象构建**:威廉斯先生通过购买财产和社会活动等方式来构建自己的社会形象,体现了个人身份认同与社会期望之间的关系。 ##### (二)道德与法律边界 1. **道德责任与法律后果**:威廉斯先生...

    高级英汉翻译理论与实践

    3. 翻译行业知识:涵盖了翻译行业概述、翻译市场情况、职业发展路径、翻译工具和资源的使用等,对翻译初学者而言非常实用。 4. 翻译准确性:通过OCR技术的案例,展示了在进行文件数字化处理时,保持文本准确性的重要...

    计算机外文翻译 英文翻译 翻译 毕业翻译

    3. **技术规范**:计算机翻译要遵循一定的技术规范,比如代码注释的格式、文档结构等,以保证译文的可读性和实用性。 4. **TCP/IP协议**:在提及互联网的历史时,TCP/IP协议是一个核心概念。TCP(传输控制协议)和...

    新视野大学英语第三版读写教程3课后翻译.pdf

    新视野大学英语第三版读写教程3课后翻译 在这个全球化的时代,全球公民意识已经成为一个非常重要的概念。全球公民是指一个人承认自己是新兴的全球社区的一分子,而且其行动对全球社区的价值打造和实践活动有所贡献...

    强大的单词翻译软件,本地翻译软件

    这种设计使得用户即使在没有网络的情况下也能进行翻译,特别适合旅行、户外活动或网络不稳定时使用。 这类软件通常具备以下特点: 1. **丰富的词汇库**:涵盖多个学科领域的专业词汇,满足用户从基础到高级的学习...

    电商英语翻译的任务型教学模式初探.pdf

    学生被分成小组,每组3-4人,不固定组长,通过平等讨论,完成任务的准备活动。任务环阶段是任务型教学的核心,学生在这一阶段将进行翻译材料的初步理解和表达,通过小组内部的讨论、互相修改译文,最终选出最佳译文...

    翻译目的论视角下跨境电商商品标题翻译的技巧与误区研究.pdf

    跨境电商是全球经济一体化的产物,它是指通过互联网实现不同国家和地区之间的商品和服务的交易活动。随着国际贸易的增长和互联网技术的普及,跨境电商在全球范围内得到了迅速的发展。而商品标题翻译作为跨境电商中...

    百度翻译;翻译API历史翻译记录 2.zip

    3. **使用统计**:每日、每月或特定时间段内的API调用次数,有助于理解用户行为模式和流量峰值。 4. **翻译对比**:不同翻译结果的比较,可能包括机器翻译与人工翻译的差异,以便改进翻译算法。 5. **用户反馈**:...

    牛津英语9A课本句子翻译练1-3.doc

    Unit 3关注的是日常生活中的平衡问题,尤其是学习和兴趣爱好之间的平衡。缺乏充足的睡眠和时间管理困难是普遍的问题,作业负担重可能影响到个人的兴趣发展。如何在学业和爱好间找到平衡,以及如何应对学习压力,成为...

    当代研究生英语读写教程1-3单元课文翻译

    "当代研究生英语读写教程1-3单元课文翻译" 该资源摘要信息是基于对当代研究生英语读写教程1-3单元课文的翻译和分析。该资源涵盖了信息空间的概念、特点和发展方向,包括信息空间的定义、用户行为、群体互动、服务...

Global site tag (gtag.js) - Google Analytics