Fragment的声明周期整理

声明周期示意图

Fragment生命周期图:

Fragment与Activity生命周期对比图:

-

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

2   生命周期分析

1. 当一个fragment被创建的时候,它会经历以下状态.

  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()

2. 当这个fragment对用户可见的时候,它会经历以下状态。

  • onStart()
  • onResume()

3. 当这个fragment进入“后台模式”的时候,它会经历以下状态。

  • onPause()
  • onStop()

4. 当这个fragment被销毁了(或者持有它的activity被销毁了),它会经历以下状态。

  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy() // 本来漏掉类这个回调,感谢xiangxue336提出。
  • onDetach()

5. 就像activitie一样,在以下的状态中,可以使用Bundle对象保存一个fragment的对象。

  • onCreate()
  • onCreateView()
  • onActivityCreated()

6. fragments的大部分状态都和activitie很相似,但fragment有一些新的状态。

  • onAttached() —— 当fragment被加入到activity时调用(在这个方法中可以获得所在的activity)。
  • onCreateView() —— 当activity要得到fragment的layout时,调用此方法,fragment在其中创建自己的layout(界面)。
  • onActivityCreated() —— 当activity的onCreated()方法返回后调用此方法
  • onDestroyView() —— 当fragment中的视图被移除的时候,调用这个方法。
  • onDetach() —— 当fragment和activity分离的时候,调用这个方法。

一旦activity进入resumed状态(也就是running状态),你就可以自由地添加和删除fragment了。因此,只有当activity在resumed状态时,fragment的生命周期才能独立的运转,其它时候是依赖于activity的生命周期变化的。

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

谷歌文档中关于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.

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.
  2. onCreate(Bundle) called to do initial creation of the 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().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment interacting with the user (based on its containing activity being resumed).

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.
  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.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Android开发中,Fragment是一个可重用的UI部分,它可以独立于Activity存在,并且可以根据需要添加到不同的Activity中。Fragment有自己的生命周期管理,这包括一系列的方法,用于描述Fragment从创建、显示、活跃到销毁的整个过程。 1. **onCreate()**: 当Fragment实例被创建时调用,这是初始化阶段,可以在此设置基本属性和数据。 2. **onCreateView()**: 如果Fragment没有预先创建视图,这个方法会被调用来生成视图。返回null则会使用默认布局。 3. **onStart()**: 当Fragment成为活动焦点或可见时调用,可以在这里开始执行耗时操作。 4. **onResume()**: 当Fragment变为活动的前景或者用户正在交互时,这个方法会被调用,意味着它是完全活跃的。 5. **onPause()**: 当Fragment不再接收用户交互或成为活动焦点时,但依然可见时,这个方法会被调用。 6. **onStop()**: 当Fragment失去焦点并且不处于可见状态时,例如切换到其他Activity,这个方法会被调用。 7. **onDestroyView()**: 当Fragment的视图不再需要时,这个方法会被调用,通常在onPause之后。 8. **onSaveInstanceState()**: 在onPause或onStop之前调用,用于保存Fragment的状态,以便在恢复时使用。 9. **onDestroy()**: 当Fragment被系统卸载,不再需要内存时,这个方法会被调用。 10. **onActivityCreated()**: 当Fragment的视图已经创建完毕,并且所有的依赖项都可用时,这个方法会被调用。 11. **onDestroyView()**: 视图销毁后调用,释放资源。 12. **onDetach()**: 当Fragment从其宿主Activity中分离出来时,这个方法会被调用,通常发生在用户切换回其他Activity时。 理解Fragment的生命周期对于管理其行为、数据和资源至关重要。开发者可以根据这些阶段进行必要的资源管理、数据保存和状态更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CnPeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值