Fragment生命周期 API中文翻译

来源:互联网 发布:数据标准该怎么制定 编辑:程序博客网 时间:2024/06/05 17:31

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity.getFragmentManager() and Fragment.getFragmentManager().

一个fragment是应用程序的一部分,它可以用Activity来替换。通过FragmentManagerfragment交互,可以使用函数Activity.getFragmentManager()  Fragment.getFragmentManager()获取fragment

The Fragment class can be used many ways to achieve a wide variety of results. It is core, it represents a particular operation or interface that is running within a largerActivity. A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed.

Fragment类可以实现很多功能。这个类的核心是:在一个大的Activity里表现一个特定的操作或接口。FragmentActivity紧密相关。虽然Fragment也有自己的生命周期,但是这个生命周期是依赖Activity的。如果Activity停止了,所有的Fragment都不可以启动;当Activity被销毁,所有的Fragment也都被销毁。

All subclasses of Fragment must include a public empty constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore.

Fragment的子类必须有一个空的构造函数。框架总是在需要的时候重实例化一个Fragment,尤其在状态恢复的时候,需要这个构造函数去实例化fragment。如果这个空的构造函数是无效的,状态恢复的时候会抛出运行时异常。

Lifecycle

Though a Fragment's lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such as onResume(), but also important are methods related to interactions with the activity and UI generation.      首先fragment是依赖于activity的

The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) called once the fragment is associated with its activity.       当fragment关联上activity时调用
  2. onCreate(Bundle) called to do initial creation of the fragment.   当初始化fragment时调用
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.       创建并返回相关的视图层级
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().   告诉该fragment他所关联的activity已经完成他本身的初始化
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.      告诉fragment他的视图层级的保存状态已经被存储
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).    fragment对用户可见(根据包含他的activity是否已经start)
  7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).   fragment可以跟用户交互 (根据包含他的activity是否已经resume)  

    As a fragment is no longer being used, it goes through a reverse series of callbacks:

    作为一个片段是不再使用,它通过一系列反向回调:

    1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.       当activity处于paused状态或者fragment正在被修改fragment将不能和用户交互
    2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.               当activity处于stop状态或者fragment正在被修改fragment将不能和用户交互
    3. onDestroyView() allows the fragment to clean up resources associated with its View.  当允许用户清理与view关联的资源
    4. onDestroy() called to do final cleanup of the fragment's state.   清理fagment的状态
    5. onDetach() called immediately prior to the fragment no longer being associated with its activity.   不再关联fragment与activity不再关联
0 0