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的生命周期鹰椒。本文檔的其余部分介紹了這些主題。