Android 中的 Activity 物件

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

原始文件:http://developer.android.com/guide/topics/fundamentals.html
授權方式:本文採用 Apache 2.0 授權,使用時請遵守該授權協議。

每個 Activity 通常會負責呈現出一個使用者介面,例如呈現出一個選單讓使用者選取。舉例而言,一個訊息發送的程式,可能有一個 Activity 負責呈現出接收者選單,第二個 Activity 才負責寫入訊息給選定的接收者,然後其他的 Activity 可能進行列出訊息或者是修改設定等工作。雖然所有程式都緊密合作,形成一個有組織的介面,但是卻都是獨立的。

An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class.

應用程式通常包含多個分工合作的 Activity,到底應該有多少 Activity 取決於你的設計。每個應用程式都會有一個起始的 Activity,程式的執行就在 Activity 的轉換過程當中完成了。

An application might consist of just one activity or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the activities is marked as the first one that should be presented to the user when the application is launched. Moving from one activity to another is accomplished by having the current activity start the next one.

每個 Activity 都有一個預設占據全螢幕的視窗畫面,但是也可以有不是填滿全螢幕的視窗,浮現在其他的視窗中,像是浮現式對話框就是一個浮動視窗的例子。

Each activity is given a default window to draw in. Typically, the window fills the screen, but it might be smaller than the screen and float on top of other windows. An activity can also make use of additional windows — for example, a pop-up dialog that calls for a user response in the midst of the activity, or a window that presents users with vital information when they select a particular item on-screen.

顯示介面的主要物件稱為 View,所有的顯示元件都從 View 繼承下來。每個 View 元件都占有一塊矩形空間,父元件 (Parent View) 容納並組織子元件。沒有子孫的樹葉元件會回應使用者的動作。所以 View 是系統與使用者互動的場所。例如某個 View 可能顯示一張小圖,在該圖被點選時會觸發某個動作。Android 有很多種預設的 View,像是按鈕、輸入框、捲軸、功能表、選項等物件。

The visual content of the window is provided by a hierarchy of views — objects derived from the base View class. Each view controls a particular rectangular space within the window. Parent views contain and organize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space. Thus, views are where the activity's interaction with the user takes place. For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of ready-made views that you can use — including buttons, text fields, scroll bars, menu items, check boxes, and more.

View 所形成的容納結構稱為視覺樹 (View Hierarchy),你可以透過 Activity.setContentView() 將視覺樹放到 Activity 的視窗中,所謂的 Content View 就是整顆視覺樹的樹根。

A view hierarchy is placed within an activity's window by the Activity.setContentView() method. The content view is the View object at the root of the hierarchy. (See the separate User Interface document for more information on views and the hierarchy.)

您可以透過 finish() 函數結束一個 Activity,或者透過呼叫 finishActivity() 以請求 Android 結束您透過 startActivityForResult() 所啟動的 Activity。

An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().

Activities and Tasks

如前所述,Activity 可以啟動該應用 (或另一個應用) 中的 Activity,例如您想用 GoogleMap 顯示某個地點,只要傳送 Intent 給 startActivity() 函數以啟動 GoogleMap 的 Activity 即可顯示該位置的地圖。當您按下返回 (BACK) 鍵後,又會回到上一個畫面中 ( 原本執行 startActivity(Intent) 函數的 Activity 中)。

As noted earlier, one activity can start another, including one defined in a different application. Suppose, for example, that you'd like to let users display a street map of some location. There's already an activity that can do that, so all your activity needs to do is put together an Intent object with the required information and pass it to startActivity(). The map viewer will display the map. When the user hits the BACK key, your activity will reappear on screen.

對於使用者來說,這讓 GoogleMap 看來就像是該應用的一部分 (但事實上不是),即便 GoogleMap 事實上是另一個應用的一部分,使用者也感覺不出來。Android 達成這種體驗的方法,是用一個 Activity 堆疊所實作出來的。使用者經驗中的 Application,就在 Activity 堆疊的推入與取出中完成了。當使用者在啟動畫面 (Launcher) 中選取某個應用後,就會創建出該應用的 Activity 堆疊,然後將該應用的起始 Activity 推入成為堆疊的第一個元素。當一個 Activity 啟動另一個 Activity 時,新進的 Activity 會被推入堆疊的頂端,當我們按下 BACK 鍵返回上一個畫面時,堆疊頂的 Activity 又被彈出堆疊,於是回到上一個畫面 (Activity)。

Te user, it will seem as if the map viewer is part of the same application as your activity, even though it's defined in another application and runs in that application's process. Android maintains this user experience by keeping both activities in the same task. Simply put, a task is what the user experiences as an "application." It's a group of related activities, arranged in a stack. The root activity in the stack is the one that began the task — typically, it's an activity the user selected in the application launcher. The activity at the top of the stack is one that's currently running — the one that is the focus for user actions. When one activity starts another, the new activity is pushed on the stack; it becomes the running activity. The previous activity remains in the stack. When the user presses the BACK key, the current activity is popped from the stack, and the previous one resumes as the running activity.

假如同一個 Activity 的畫面被啟動兩次,甚至是多次,都沒有關係。舉例而言,假如啟動了很多個 GoogleMap 畫面分別顯示不同的位置,那麼堆疊中就會有很多個 GoogleMap 的 Activity,但是這並沒有關係,您也不需特別去安排 Activity 在堆疊中的順序,堆疊中只要有 push 和 pop 動作就夠了。

The stack contains objects, so if a task has more than one instance of the same Activity subclass open — multiple map viewers, for example — the stack has a separate entry for each instance. Activities in the stack are never rearranged, only pushed and popped.

在 Android 中的每個 Task 都是一個 Activity 堆疊,而不是 AndroidManifest.xml 中的一個類別或元素。所以你不能在某個 Activity 中設定全域變數值,只能將全域變數值放在 root activity 中。在下一個單元中,我們將會看到行程的姻親 (affinity) 這個概念,你可以在 root activity 當中存放姻親值並讀取出來。

A task is a stack of activities, not a class or an element in the manifest file. So there's no way to set values for a task independently of its activities. Values for the task as a whole are set in the root activity. For example, the next section will talk about the "affinity of a task"; that value is read from the affinity set for the task's root activity.

All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed.

Activity 的生命週期

activity_lifecycle.png

圖一、Activity 的生命週期狀態圖

尚未翻譯的部分

The behavior just described is the default behavior for activities and tasks. But there are ways to modify almost all aspects of it. The association of activities with tasks, and the behavior of an activity within a task, is controlled by the interaction between flags set in the Intent object that started the activity and attributes set in the activity's <activity> element in the manifest. Both requester and respondent have a say in what happens.

In this regard, the principal Intent flags are:

FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP

The principal <activity> attributes are:

taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch

The following sections describe what some of these flags and attributes do, how they interact, and what considerations should govern their use.

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License