2019-04-08 Introduction to Activities

Introduction to Activities

The Activity class is a crucial component of an Android app, and the way activities are launched and put together is a fundamental part of the platform's application model. Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

Activity是Android應用中一個十分重要的組件,并且它的啟動和組合方式是應用模式平臺的基本組成部分甸赃。與使用main()方法啟動應用程序的編程范例不同悔耘,Android系統(tǒng)通過調(diào)用與活動實例生命周期的特定階段對應的特定回調(diào)方法來啟動活動實例中的代碼倡勇。

This document introduces the concept of activities, and then provides some lightweight guidance about how to work with them. For additional information about best practices in architecting your app, see Guide to App Architecture.
該文檔介紹的Activity的概念川蒙,并且提供了一些關于如何讓它們工作的輕量級的指導录语,有關應用程序架構最佳實踐的更多信息芭逝,請閱讀 Guide to App Architecture.

The concept of activities

Activity概念
The mobile-app experience differs from its desktop counterpart in that a user's interaction with the app doesn't always begin in the same place. Instead, the user journey often begins non-deterministically. For instance, if you open an email app from your home screen, you might see a list of emails. By contrast, if you are using a social media app that then launches your email app, you might go directly to the email app's screen for composing an email.
移動應用程序體驗與桌面應用程序的不同之處在于荡灾,用戶與應用程序的交互并不總是在同一位置開始毡惜。相反拓轻,用戶往往是從非確定性的地方開始旅程。例如经伙,如果你從首頁打開一個郵件app扶叉,你可能會看到一個郵件列表勿锅。相比之下,如果你使用的是社交媒體應用程序枣氧,然后啟動你的電子郵件應用程序溢十,你可以直接進入電子郵件應用程序的屏幕撰寫電子郵件。
The [Activity](https://developer.android.google.cn/reference/android/app/Activity.html) class is designed to facilitate this paradigm. When one app invokes another, the calling app invokes an activity in the other app, rather than the app as an atomic whole. In this way, the activity serves as the entry point for an app's interaction with the user. You implement an activity as a subclass of the [Activity](https://developer.android.google.cn/reference/android/app/Activity.html) class.
Activity類被設計為促進這樣的范式达吞。當一個app調(diào)用另一個app時张弛,調(diào)用app去喚醒另一個app里面的Activity,而不是把app當作一個原子整體調(diào)用酪劫。在這種方式下吞鸭,Activity就成為app與用戶交互的入口點。你可以實現(xiàn)一個Activity的子類(去做自定義的操作)覆糟。
An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app. For instance, one of an app’s activities may implement a Preferences screen, while another activity implements a Select Photo screen.
Activity提供app繪制其用戶界面的窗口刻剥。這個窗口通常充滿屏幕,但也可能小于屏幕并浮動在其他窗口之上滩字。通常造虏,一個活動在應用程序中實現(xiàn)一個屏幕。例如踢械,app的一個Activity可以實現(xiàn)首選項屏幕酗电,而另一個Activity可以實現(xiàn)選擇照片屏幕。
Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions. For example, the main activity in a simple e-mail app may provide the screen that shows an e-mail inbox. From there, the main activity might launch other activities that provide screens for tasks like writing e-mails and opening individual e-mails.
大多數(shù)app包含多個屏幕内列,這意味著它包含多個Activity撵术。通常,app有一個Activity被指定為主Activity话瞧,當用戶啟動app時它是第一個顯示的屏幕嫩与。然后,每個Activity可以啟動另一個Activity去執(zhí)行不同的操作交排。簡單電子郵件應用程序中的主Activity可能提供顯示電子郵件收件箱的屏幕划滋。從那里,主Activity可能會啟動其他活動埃篓,為諸如編寫電子郵件和打開單個電子郵件等任務提供屏幕处坪。
Although activities work together to form a cohesive user experience in an app, each activity is only loosely bound to the other activities; there are usually minimal dependencies among the activities in an app. In fact, activities often start up activities belonging to other apps. For example, a browser app might launch the Share activity of a social-media app.
盡管Activity在app中共同工作形成一種緊密的用戶體驗,但是每個Activity僅僅是與其他的Activity松散的綁定在一起架专;app中的Activity通常存在最小的依賴關系同窘。事實上,Activity經(jīng)常啟動屬于其他app的Activity部脚。例如想邦,瀏覽器app可能會啟動社交媒體app的共享Activity。
To use activities in your app, you must register information about them in the app’s manifest, and you must manage activity lifecycles appropriately. The rest of this document introduces these subjects.
為了在你的app中使用Activity委刘,你必須在app的清單文件中注冊他們的有關信息丧没,并且你必須適當?shù)毓芾鞟ctivity的生命周期鹰椒。本文檔的其余部分介紹了這些主題。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呕童,一起剝皮案震驚了整個濱河市漆际,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拉庵,老刑警劉巖灿椅,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異钞支,居然都是意外死亡,警方通過查閱死者的電腦和手機操刀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門烁挟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人骨坑,你說我怎么就攤上這事撼嗓。” “怎么了欢唾?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵且警,是天一觀的道長。 經(jīng)常有香客問我礁遣,道長斑芜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任祟霍,我火速辦了婚禮杏头,結果婚禮上,老公的妹妹穿的比我還像新娘沸呐。我一直安慰自己醇王,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布崭添。 她就那樣靜靜地躺著寓娩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呼渣。 梳的紋絲不亂的頭發(fā)上棘伴,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音徙邻,去河邊找鬼排嫌。 笑死,一個胖子當著我的面吹牛缰犁,可吹牛的內(nèi)容都是我干的淳地。 我是一名探鬼主播怖糊,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼颇象!你這毒婦竟也來了伍伤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤遣钳,失蹤者是張志新(化名)和其女友劉穎扰魂,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蕴茴,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡劝评,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了倦淀。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒋畜。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖撞叽,靈堂內(nèi)的尸體忽然破棺而出姻成,到底是詐尸還是另有隱情,我是刑警寧澤愿棋,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布科展,位于F島的核電站,受9級特大地震影響糠雨,放射性物質(zhì)發(fā)生泄漏才睹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一见秤、第九天 我趴在偏房一處隱蔽的房頂上張望砂竖。 院中可真熱鬧,春花似錦鹃答、人聲如沸乎澄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽置济。三九已至,卻和暖如春锋八,著一層夾襖步出監(jiān)牢的瞬間浙于,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工挟纱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留羞酗,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓紊服,卻偏偏與公主長得像檀轨,于是被迫代替她去往敵國和親胸竞。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容