Activity介绍

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 app十分重要的一个组件,Activity的启动和组装方式是平台应用模型的基本部分,app的启动不同于平常编程范式伴随着main()方法,Android系统的初始化代码在Activity实例中唤醒特定的回调方法去响应Activity的特定生命周期阶段。

This document introduces the concept of activities, and then provides some lightweight guidance about how to work with them.

本文档介绍了活动的概念,并且提供了一些关于如何使用他们的轻量级指导。

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.

移动应用程序的体验不同于它的桌面对应,因为用户与应用程序的交互并不总是在同一个地方开始。相反,用户开始(交互的地方)往往是不确定的。例如,如果你从你的主屏幕打开一个电子邮件应用程序,你可能会看到一个电子邮件列表。相比之下,如果你使用的是一个社交媒体应用程序,然后启动你的电子邮件应用程序,你可能会直接进入电子邮件应用程序撰写电子邮件的屏幕

The Activity 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 class.

Activity类被设计作为促进这一范式:当app调用其他的app时,该app调用的是其他app的Activity,而不是将被调用app作为一个原子整体(来调用)。Activity以这种方式作为用户和app交互的入口点,你可以实现一个Activity作为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绘制UI的窗口。该窗口通常充满屏幕,但是也可能小于屏幕并可漂浮于其他窗口之上。通常,在一个app中一个Activity实现一个屏幕。app的一个Activity可能实现一个首选项屏幕,而另一个Activity实现选择照片屏幕。

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.

大多数app包含多个屏幕,这意味着他们包含多个Activity。通常,在app中有一个Activity被指定为main Activity,它是用户启动app时出现的第一个屏幕,每个Activity能启动另一个Activity来执行不同的动作。例如,一个简单的e-mail app可能在main Activity显示一个收件箱。在那儿,main 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通常启动属于其他appActivity。例如,一个浏览器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的manifest文件中注册他们的有关信息,并且你必须适当的控制Activity的生命周期。本文档的其余部分介绍了这些主题。

Configuring the manifest

配置manifest文件

For your app to be able to use activities, you must declare the activities, and certain of their attributes, in the manifest

为了你的app能够使用Activity,你必须在manifest文件中声明Activity和确定他们的属性。

Declare activities

声明Activity

To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:

为了声明你的Activity,(你需要)打开你的manifest文件并且添加一个<activity>元素作为<application>元素的子节点,如下:

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

The only required attribute for this element is android:name, which specifies the class name of the activity. You can also add attributes that define activity characteristics such as label, icon, or UI theme. For more information about these and other attributes, see the <activity> element reference documentation.

必需的属性是android:name这个元素,它确定了Activity的类名。你也可以添加Activity定义的特性如标签,图标或者UI主题。想要了解更多关于这些的信息和其他属性,请看<activity> 元素引用的文档

Note: After you publish your app, you should not change activity names. If you do, you might break some functionality, such as app shortcuts. For more information on changes to avoid after publishing, see Things That Cannot Change.

注意:在你发布你的app之后,你不应该改变Activity的名字。如果你这样做了,那可能会损坏一些功能,例如app的快捷键。更多的关于避免发布后的改变信息,请看Things That Cannot Change

Declare intent filters

声明intent filters

Intent filters are a very powerful feature of the Android platform. They provide the ability to launch an activity based not only on an explicit request, but also an implicit one. For example, an explicit request might tell the system to “Start the Send Email activity in the Gmail app". By contrast, an implicit request tells the system to “Start a Send Email screen in any activity that can do the job." When the system UI asks a user which app to use in performing a task, that’s an intent filter at work.

Intent filters 是Android平台的一个非常强大的功能。他们不但提供了启动Activity的显式请求,而且也提供了隐式请求。例如,显示请求可能去告诉系统“在Gmail app中启动发送邮件Activity”。作为对比,一个隐式请求告诉系统“在任何可以做这项工作的活动中启动一个发送电子邮件屏幕”。当系统UI询问用户使用哪个app执行一项任务时,那是intent filter在工作。

You can take advantage of this feature by declaring an <intent-filter> attribute in the <activity> element. The definition of this element includes an<action> element and, optionally, a <category> element and/or a <data> element. These elements combine to specify the type of intent to which your activity can respond. For example, the following code snippet shows how to configure an activity that sends text data, and receives requests from other activities to do so:

你可以通过在<activity>元素中声明<intent-filter>属性来使用这项功能。<intent-filter>定义的元素包括<action>元素和可选的<category>元素和/或<data>元素。这些元素结合了能被你的Activity响应的具体的intent类型。例如,以下代码段展示了如何配置一个发送文本数据的Activity,并且接收来自其他Activity的请求:

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

In this example, the <action> element specifies that this activity sends data. Declaring the <category> element as DEFAULT enables the activity to receive launch requests. The <data> element specifies the type of data that this activity can send. The following code snippet shows how to call the activity described above:

在这个示例中,<action> 元素指定了该Activity发送的数据。声明<category>元素为默认值以便该Activity能够接收启动请求。<data>元素指定该Activity能够发送的数据类型。以下代码段显示了如何去调用上面描述的Activity:

KOTLIN

val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    type = "text/plain"
    putExtra(Intent.EXTRA_TEXT, textMessage)
}
startActivity(sendIntent)

JAVA

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
// Start the activity
startActivity(sendIntent);


If you intend for your app to be self-contained and not allow other apps to activate its activities, you don't need any other intent filters. 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. For more information about how your activities can respond to intents, see Intents and Intent Filters.

如果你想让你的app是独立的,不允许其他app激活它的Activity,那么你不需要任何其他intent filters。您不想为其他应用程序提供的Activity应该没有intent filters,您可以使用显式intent自己启动它们。有关Activity如何响应Intent的更多信息,请参见Intents and Intent Filters




猜你喜欢

转载自blog.csdn.net/agoddog/article/details/79656087