Creating an Activity创建一个Activity

  • Android,activity,翻译,中文,英文,官方文档,


To create an activity, you must create a subclass of Activity (or an existing subclass of it).

创建一个 activity,必须创建一个 Activity 的子类(或者一个已经存在的 Activity 的子类)。总之,这个类 肯定是 Activity 的子类。

In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed.

在你的子类里,你需要实现回调方法,当 activity 在它的生命周期的不同(various)状态之间转换 (transition)的时候,系统会调用这些回调方法,比如 activity 被创建,停止,恢复或者销毁。

The two most important callback methods are:



You must implement this method. The system calls this when creating your activity.


Within your implementation, you should initialize the essential components of your activity.

在你实现的方法内,你可以初始化这个 activity 的主要(essential)的控件(component)。

Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.

最重要的,在这个方法里,你需要调用 setContentView() 来定义(define)这个 activity 的用户交互界面的布局。


The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed).

系统会首先调用这个方法来表示用户正在离开你的 activity(虽然它并不总是意味着这个activity正在被销毁 )

This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).


There are several other lifecycle callback methods that you should use in order to provide a fluid user experience between activities and handle unexpected interruptions that cause your activity to be stopped and even destroyed.

还有一些其他的生命周期的回调方法,你可以使用它们在 activity 之间来提供一个流畅(fluid)的用户体验 (user experience)和处理一些意外(unexpected)的中断(interruptions),这些中断可能导致你的activity 被停止甚至销毁。

All of the lifecycle callback methods are discussed later, in the section about Managing the Activity Lifecycle.


返回总目录】 【返回Activity


- EOF -

转载请注明:文章转载自 Binkery 技术博客 []
本文标题: Creating an Activity创建一个Activity