网上有的博客写得太乱
不如自己翻译官方文档
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.
虽然Fragmentd的生命周期依赖于拥有它的Activity,但在Activity的生命周期标准上他有自己的一套周期,包括基本的activity生命周期方法,比如onResume(),这些方法也都与activity和ui相互交互。
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
将fragment从运行到resumed状态(与用户交互)的生命周期核心方法是:
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
创建和返回和fragment有关的视图层级。
4.onActivityCreated(Bundle)
tells the fragment that its activity has completed its own Activity.onCreate()
.
告诉fragment它关联的activity已经完成了自己的onCreate()方法。
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可见时调用。
7,onResume()
makes the fragment interacting with the user (based on its containing activity being resumed).
fragment可以和用户交互
As a fragment is no longer being used, it goes through a reverse series of callback:
当fragment不再使用时,经理一系列反向回调。
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.
fragment不再与用户交互,要么因为它关联的activity进入pasused状态,要么因为这个activity正在修改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.
fragment不再可见,要么因为它关联的activity正在进入stop状态,要么因为这个activity正在修改fragment的操作
3,onDestroyView()
allows the fragment to clean up resources associated with its View.
运行fragment与它相关视图的资源
4,onDestroy()
called to do final cleanup of the fragment's state.
清除fragment的状态。
5,onDetach()
called immediately prior to the fragment no longer being associated with its activity
在Fragment不再和Activity关联之前调用onDetach()