Activity (1)

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.

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是app的一个重要部分,且activity的发布方式和activity间的结合是平台中的一个重要部分。不像其他程序是在main()方法中启动的,android系统通过回调activity生命周期中的一个回调函数来初始化代码。

这个文档介绍了activity的概念,提供了一些使用activity的例子。对于如何更好地建设你的app,请参考 guide to app achitecture。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

concept of 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与其台式机在用户交互中不同的是,app不总是从相同的地方开始启动。相反,用户总是从不确定的地方启动app。例如,如果你从首页启动了一个email app,你也许会看见一个email的列表。相反的,如果你使用社交app来拉起你的email app,你也许会直接到email app的编写 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作为一个原子性的整体一样不可分割。在这个角度看来,acitivity相当于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的窗口。这个窗口一般会充满屏幕,但也可能比屏幕更小,位于其他window的上面。一般来说,一个acitivity会实现一个app的一个屏幕。例如,一个app的acitvity会实现一个Preference screen,或者另一个activity会实现一个select photo screen。

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会包括不同的屏幕,这表示这些app包括多个activity。一般来说,一个app中会有一个activity充当main activity的角色,这个main activity是当用户打开app时看到的第一个页面。每一个acitivty可以拉起其他的acitivty去表示不同的意图。比如说,main activity是一个简单的email app中的一个展示emai 收件箱的页面 。从这个页面,main activity会拉起其他的activity(提供写email或打开一个单一的email的任务)。

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是组合在一起来提供用户体验的,每个acitivty与其他的acitivity都是轻耦合的。在app里面,activity之间总是有少数的依赖。事实上,activity总是拉起其他应用中的activity,例如浏览器的app总是拉起社交应用的share 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的生命周期。这个文当的剩下部分将介绍这些部分的信息。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

Declare activities

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


  
      
      ...
  
  ...


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  element reference documentation.

声明一个acitivity只需要指明一个属性,android:name,用来指明activity的类名。你也可以添加一些属性来定义acitivity的一些特性,比如它的label,icon,或者它的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.

Declare 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 filter是一个非常有用的功能。他们提供显式和隐式的两个方式来拉起另一个activity。比如一个显式的intent会告诉系统“拉起gmail app中的发送email的acitivity”。相反的,一个隐式的intent会告诉系统 “开启任意一个可以发送email的activity”。当系统ui询问用户想要开启哪一个app去做这项工作,这就是一个intent filter在工作。

You can take advantage of this feature by declaring an  attribute in the  element. The definition of this element includes an  element and, optionally, a  element and/or a  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:

你可以通过在acitivity中声明Intent-filter来利用这个功能。intent-filter中的声明包括元素,和可选的。这些元素联合在一起去识别你的activity可以响应哪些种类的intent。比如,下面的这些代码展示怎么配置一个发送text data的activity,且接收来自其他的activity请求的。


    
        
        
        
    


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

比如:指定了这个activity是发送数据的,声明元素为default允许该activity接受其他acitivity的请求,指定activity可以发送的数据类型。下面的code可以展示怎么调用上面声明的这个activity。

KOTLINJAVA

// 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 filter,而使用显式的intent来开启其他的activity。

(一篇文章真的好长啊。。。。。,翻译地我都有点累了。。)

Declare permissions

You can use the manifest's  tag to control which apps can start a particular activity. A parent activity cannot launch a child activity unless both activities have the same permissions in their manifest. If you declare a  element for a parent activity, each child activity must have a matching  element.

你可以使用manifest中的标签来控制哪些app可以开启特定的activity。一个parent activity只能在parent activity和child activity有相同的权限的情况下才能调起child activity。

For example, if your app wants to use a hypothetical app named SocialApp to share a post (发布帖子)on social media, SocialApp itself must define the permission that an app calling it must have:

比如如果你的app想利用一个叫socialApp来在社交媒体上发布帖子,SocialApp本身必须定义调用它的应用程序必须具有的权限。



(这部分写在socialApp中)

Then, to be allowed to call SocialApp, your app must match the permission set in SocialApp's manifest:

而为了你可以调起socialApp,你的app需要拥有socailApp中定义的权限。


   

(这部分写在你的app里)

For more information on permissions and security in general, see Security and Permissions.

(关于更多有关权限与安全的事情,可以查看Security and Permissions)。

Managing the activity lifecycle

Over the course of its lifetime, an activity goes through a number of states. You use a series of callbacks to handle transitions between states. The following sections introduce these callbacks.

在整个activity的生命区间,一个acitivity需要经历一系列的状态。你使用一系列的回调函数来处理状态变换之间的过渡。下面会介绍这些回调函数。

onCreate()

You must implement this callback, which fires when the system creates your activity. Your implementation should initialize the essential components of your activity: For example, your app should create views and bind data to lists here. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.

When onCreate() finishes, the next callback is always onStart().

你必须实现这个回调函数,这会在系统创建activity的时候触发。你应该在这个回调函数里面初始化activity的相关配置。比如你的app应该在这里创建view,绑定列表的数据。更重要的是,你需要在这里调setContentView来定义这个activity使用的布局。

当onCreate完成后,会调用onStart()

onStart()

As onCreate() exits, the activity enters the Started state, and the activity becomes visible to the user. This callback contains what amounts to the activity’s final preparations for coming to the foreground and becoming interactive.

这个回调函数主要做activity在对用户可见前的最后准备。

这意味着onStart主管可见性。

onResume()

The system invokes this callback just before the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, and captures all user input. Most of an app’s core functionality is implemented in the onResume() method.

The onPause() callback always follows onResume().

系统在acitivty与用户可交互之前调用这个回调函数。这时候,actiivty在activity栈中的最顶端,并且获取用户的所有输入。app的主要功能都是在onResume方法中执行。onPause的回调函数总是在onResume()之后。

这意味着,onResume主管可交互。

onPause()

The system calls onPause() when the activity loses focus and enters a Paused state. This state occurs when, for example, the user taps the Back or Recents button. When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity, and the activity will soon enter the Stopped or Resumed state.

系统会在acitivty失去foucus之后,调用onPause进入暂停状态。这个状态一般会在用户点击返回或最近按钮的时候出现,这一般意味着你的actiivty仍然处于可视状态,但会意味着用户准备离开这个activitry,且该activity很快会进入stopped或resumed状态。

An activity in the Paused state may continue to update the UI if the user is expecting the UI to update. Examples of such an activity include one showing a navigation map screen or a media player playing. Even if such activities lose focus, the user expects their UI to continue updating.

一个actiivty处于暂停状态也可能会持续更新它的Ui,如果用户是希望ui更新的。

You should not use onPause() to save application or user data, make network calls, or execute database transactions. For information about saving data, see Saving and restoring activity state.

你不应该在onPause的时候,保存用户数据,做网络请求,或者执行数据库的交互。对于保存数据,可以看Saving and restoring activity state.

Once onPause() finishes executing, the next callback is either onStop() or onResume(), depending on what happens after the activity enters the Paused state.

一旦onPause执行完成,下一个回调函数就是onStop或onResume,决定于activity在onPause状态之下发生了什么。

onStop()

The system calls onStop() when the activity is no longer visible to the user. This may happen because the activity is being destroyed, a new activity is starting, or an existing activity is entering a Resumed state and is covering the stopped activity. In all of these cases, the stopped activity is no longer visible at all. 

系统会在acitivity不再对用户可见的情况下调用onStop。这会在activity准备被销毁之前发生,一个新的activity正在新建,或一个已存在的acitivty正在进入onResume状态,这都将会覆盖这个处于stopped状态的activity。在所有的这些情况中,activity都是不再对用户可见。

The next callback that the system calls is either onRestart(), if the activity is coming back to interact with the user, or by onDestroy() if this activity is completely terminating.

剩下的回调函数系统会调用的是onRestart(),如果这个acitivty将会重新与用户再次交互,或者

onRestart()

The system invokes this callback when an activity in the Stopped state is about to restart. onRestart() restores the state of the activity from the time that it was stopped.

This callback is always followed by onStart().

onDestroy()

The system invokes this callback before an activity is destroyed.

This callback is the final one that the activity receives. onDestroy() is usually implemented to ensure that all of an activity’s resources are released when the activity, or the process containing it, is destroyed.

This section provides only an introduction to this topic. For a more detailed treatment of the activity lifecycle and its callbacks, see The Activity Lifecycle.

参考链接:https://developer.android.com/guide/components/activities/intro-activities#java

你可能感兴趣的:(android,developer)