【正文】
tivity 后面有 activity, task 后面還會(huì)有 task。 activity 棧里面存放的只能是 activity,而視圖、窗體、菜單和對(duì)話(huà)框則不能。啟動(dòng) activity 一般有這 么幾種方式,應(yīng)用程序發(fā)射器、桌面快捷方式、最近任務(wù)切換器。 當(dāng)然,也有例外的。 此外,也不是所有 activity 都是當(dāng)按下 BACK 鍵之后銷(xiāo)毀掉的。這種一般發(fā)生在 activity A 比 activity B 的功能更為強(qiáng)大一些。這時(shí)用戶(hù)打開(kāi)了日歷 activity,比如查看今天是星期幾。注意相冊(cè)是另外的一個(gè)應(yīng)用程序。 設(shè)計(jì)小貼士 東北大學(xué) 東軟信息學(xué)院畢業(yè)設(shè)計(jì)(論文) 譯文 4 下面的提示和指導(dǎo)都 是針對(duì)應(yīng)用設(shè)計(jì)者和開(kāi)發(fā)者而提出的。許多應(yīng)用程序中的數(shù)據(jù)都需要共享給其它用戶(hù)的,例如, 、文本消息、上傳下載等。(這種方式需使用 startActivityForResult()) 啟動(dòng)一個(gè) activity 不需要返回結(jié)果 官方稱(chēng)這種方式為 openended。(這個(gè)例子的兩個(gè)應(yīng)用它們所在的兩個(gè)包是 和 ,有興趣的可以深入研東北大學(xué) 東軟信息學(xué)院畢業(yè)設(shè)計(jì)(論文) 譯文 5 究)。通知系統(tǒng)在這方面就做得就比較好,用戶(hù)可以在適當(dāng)?shù)臅r(shí)候從屏幕上方拉下通知列表以便回應(yīng)消息??偨Y(jié)一句話(huà)就是,當(dāng)前窗體是從先前的窗體上跳轉(zhuǎn)過(guò)來(lái)的,這時(shí)按 BACK 鍵就會(huì)回到了先前的窗體。s work connection and allows the map to continue loading in the background. State 3 The map activity is now running in the background, with Home in the foreground. The user then launches the Calendar activity, which launches into the foreground, taking user focus, where they view today39。re doing is publishing access to your activity, which means you can cause unintentional security holes. Consider how you want your activities to be launched or used by other applications As a designer or developer, it39。 the user can return by pressing the BACK key. (Such an activity is started with startActivity().) Launch two or more main activities within a single application from separate icon at Home As we have defined it, all the code in a single .apk file is considered to be one application. You can write an application that contains two main activities launchable from Home. The application is a good example of an application that contains two independent main activities — Camera and Camcorder — that each have their own icons in application launcher, that can be launched separately, and so appear to the user as separate applications. They both share use of the same lens, and both store their images (still and moving) in the Gallery. In order for your application to contain two different, independent activities launchable from Home, you must define them to be associated with different tasks. (This means 東北大學(xué) 東軟信息學(xué)院畢業(yè)設(shè)計(jì)(論文) 譯文 13 setting the main activity for each task to a different task affinity — in this case, and .) Contacts and Dialer are another example of two main activities launchable from Home that reside in the same application. Making your application available as a widget An application can also display a portion of itself as an app widget, embedded in Home or another application, and receive periodic updates. Allow your activities to be added to the current task If your activities can be started from another application, allow them to be added to the current task (or an existing task it has an affinity with). Having activities added to a task enables the user to switch between a task that contains your activities and other tasks. Exceptions are your activities that have only one instance. For this behavior, your activity should have a launch mode of standard or singleTop rather than singleTask or singleInstance. These modes also enable multiple instances of your activity to be run. Use the notification system — don39。re done with the first task. Design Tips The following are tips and guidelines for application designers and developers. When writing an activity that won39。 這些例子就是你有絕對(duì)的需要才要重新設(shè)置 BACK 鍵功能的理由。大多數(shù)的 activity 都是一些比較通用的操作,諸如查看聯(lián)系人列表,查看照片等,如果按 BACK 鍵,就直接返回先前調(diào)用它的 activity 就好了,不需其它的功能需求。 應(yīng)用程序部件 我們也可以將應(yīng)用程序以部件的形式嵌進(jìn)桌面上或是其它應(yīng)用程序中并能它們持續(xù)更新。(這種方式需使用 startActivity()) 不同的圖 標(biāo)能夠啟動(dòng)多個(gè)相同的應(yīng)用程序 由于 Android 應(yīng)用程序的運(yùn)行代碼均存在于 .apk 文件中,因此就把這個(gè)文件看作是一個(gè)應(yīng)用程序。舉一個(gè)具體的例子,Gallery(相冊(cè)),它能讓用戶(hù)查看并共享圖片,這時(shí)用戶(hù)選擇了“共享”菜單 , Android系統(tǒng)會(huì)在 intent filter 中尋找適合該請(qǐng)求的 activity,如果有多個(gè),就會(huì)以列表的形式展現(xiàn)給用戶(hù),供其選擇。這樣的話(huà),你的 activity 就只會(huì)在應(yīng)用程序內(nèi)部來(lái)啟動(dòng)了,同樣也避免了安全漏洞。 。 以上兩個(gè)應(yīng)用程序“地圖”和“日歷”是兩個(gè)不同的任務(wù),因此 Android 支持多任務(wù)模式。這個(gè)例子中的聯(lián)系人應(yīng)用程序重 用了 activity, A和 B 雖然是完全不同的 activity,但是它們兩個(gè)彼此形成了互補(bǔ),使程序更加的強(qiáng)大。即使它的 activity 不再可見(jiàn),音樂(lè)應(yīng)用程序依然會(huì)在狀態(tài)欄上提示著用戶(hù)。例如,在郵件中以新任務(wù)的方式打開(kāi)地圖 activity 或是打開(kāi)一個(gè)瀏覽器 activity,當(dāng)按 BACK 鍵時(shí)就又會(huì)回到郵件 activity 中。 Activity ??梢允嵌鄠€(gè)任務(wù)的組成部分。有個(gè)例外情況就是,你的應(yīng)用程序需要利用 BACK鍵控制自身的導(dǎo)航,那就要自己重新設(shè)定 BACK 鍵的導(dǎo)航功能。每個(gè) activity 都有其各自的生命周期,互不干擾;并且它們都可以被用戶(hù)或者系統(tǒng)獨(dú)立啟動(dòng)、運(yùn)行、暫停、恢復(fù)運(yùn)行、停止、重新開(kāi)始。 一般情況下,桌面上都 會(huì)有 Android 應(yīng)用程序快捷圖標(biāo),用戶(hù)可以選擇某一個(gè)圖標(biāo)來(lái)啟動(dòng)應(yīng)用程序。 下面用例子來(lái)闡述了 activities 和 tasks 的一些 底層原則和機(jī)制 ,例如導(dǎo)航,多任務(wù), activity 重用,意圖和 activity 棧。你可以自己在 Android 模擬器上或是 Android 手機(jī)上去試驗(yàn)這些例子。每個(gè) activity 都有其獨(dú)特的 UI、明確的功能,諸如打電話(huà)、看照片、聽(tīng)音樂(lè)等。 Activity Stack 用戶(hù)之所以能夠從一個(gè) activity 轉(zhuǎn)到下一個(gè) activity,是因?yàn)?Android 系統(tǒng)針對(duì)東北大學(xué) 東軟信息學(xué)院畢業(yè)設(shè)計(jì)(論文) 譯文 2 activity 而設(shè)計(jì)了一個(gè)線(xiàn)性的導(dǎo)航歷史以供用戶(hù)追溯訪(fǎng)問(wèn),這就是 activity 棧,也稱(chēng)為back stack。舉個(gè)例子,用戶(hù)打開(kāi)了聯(lián)系人的程序,任務(wù)隨之也會(huì)啟動(dòng),他選擇了 地址準(zhǔn)備發(fā)郵件,這時(shí)跳轉(zhuǎn)到了 activity,之后他要添加附件,需要在相冊(cè)中挑選圖片。 這里有兩種情況來(lái)開(kāi)始其它任務(wù),并且都可 以返回到原先的任務(wù)上。 在桌面上開(kāi)始一個(gè) Activity 桌面是啟動(dòng)應(yīng)用程序的主要地方,比如在桌面上點(diǎn)擊應(yīng)用程序圖標(biāo)就能將其打開(kāi),用戶(hù)第一眼看到的就是應(yīng)用程序中的主 activity。 A 的部分功能需 要調(diào)用 B 的已實(shí)現(xiàn)功能,那么 B 就叫被重用。這時(shí),用戶(hù)該說(shuō)了,網(wǎng)絡(luò)太慢了!因?yàn)榈貓D定位是需要一些時(shí)間的。你想要發(fā)送一條短消息并附加一張圖片。 。 思考:以怎樣的方式來(lái)啟動(dòng) activity 做為 Android 設(shè)計(jì)者或開(kāi)發(fā)者,完全取決于用戶(hù)如何啟動(dòng)你的應(yīng)用程序,而應(yīng)用程序則是由一系列的 activity 組成,用戶(hù)會(huì)從 Home 或是其他應(yīng)用程序中啟動(dòng)這些 activity。 啟動(dòng)一個(gè) activity 并需要返回一個(gè)結(jié)果 官方稱(chēng)這種方式為 closed loop,也就是說(shuō)當(dāng)啟動(dòng)一個(gè) activity 之后,會(huì)返回一個(gè)結(jié)果回來(lái)。它們都共享使用一個(gè)鏡頭、在 Gallery 里面保存圖片等。 對(duì)于這種行為,你的 activity 應(yīng)該有一個(gè) standard 或 singleTop 的啟動(dòng)模式,而不是 singleTask 或 singleInstance,這樣,你的 activity 就會(huì)以多實(shí)例的模式來(lái)運(yùn)行。地圖應(yīng)用 程序?qū)⑦@些圖層面板保存在自身的歷史記錄里面,所以需要 BACK 鍵來(lái)進(jìn)行回退導(dǎo)航。re doing (their task) to perform a different task, then are able to return to where they left off to plete the original task. The idea is that users can run multiple tasks simultaneously and switch between them. There are two primary ways to jump off to that other task — in both cases the user should be able to return to where they were before the interru