Fragment 的深入思考

概述

这篇博客,是关于Fragment的深入思考,不是fragment的入门文章,如果不会使用Fragment的,请点击下面的官方链接:

https://developer.android.google.cn/guide/components/fragments.html?hl=zh-cn

官方的说明文档都翻译成了中文了。英语不会的孩子也能看的懂。

fragment的生命周期

Fragment 是寄宿在Activity中的,实际上你可以把Fragment 看着是是一个 View。因此,Activity生命周期直接影响到Fragment。

其次,Fragment 作为一个独立的个体,有着自己的生命周期,这个生命周期非常的复杂。

  • onAttach
  • onCreate
  • onCreateView
  • onActivityCreated
  • onStart
  • onResume
  • Fragment 处于active 的状态
  • onPause
  • onStop
  • onDestroyView
  • onDestroy
  • onDetach

不用死记硬背这些东西,用的多了就了解了。

FragmentTransition 中的几个关键方法区别

replace , add, remove

我们查看官方的文档,可以看到官方对这两个方法的解释如下:

add 注释如下

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) inserted into a container view of the activity.

第一句中的state 是一个形容词,意思是“正式的”。

简单的说,就是添加一个fragment到Activity指定的容器view中。

replace 注释

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

请注意最后一句 replace 等同于,本质上先remove添加到这个容器view的所有Fragment,然后在添加指定的Fragment到这个容器view。

也就是说,replace实际上走了二步,第一步,先remove所有的Fragment,然后在添加指定的Fragment。

remove 就不用说了,直接移除指定的Fragment,并且Fragment的实例也被销毁了。如果想重新加入,那么只能是 new 一个新对象了。

关于 hide 和 show 方法

把Fragment看着是一个view,那么hide可以等同于view 的:

View.setVisibility(View.INVISIBLE);

那么 show 看作是:

View.setVisibility(View.VISIBLE);

实际上Fragment 的hide 和show 不会走Fragment的任何生命周期。

这样很理解。

关于 detach 和 remove 区别

Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.

请注意这句话“This is the same state as when it is put on the back stack”

也就是说,这个Fragment 移除了,但是Fragment的状态会被Activity管理起来。尽管其视图层被摧毁了。

那 对于 attach,只有使用了 detache 移除的Fragment,才能使用 attach 重新绑定,重建视图层。

remove 会销毁整个Fragment 实例,而不仅仅是 销毁了视图。attach 则只是销毁视图,不会销毁整个Fragment的实例。

FragmentStatePagerAdapter 和 FragmentPagerAdapter

简单的说,FragmentStatePagerAdapter,会保留每一个Fragment的状态,在内部,有一个Fragment的链表和 Fragment.SavedState 的链表,保证了其Fragment 的状态一定不会丢失。

下面是二行关键代码


 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();

private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    

FragmentPagerAdapetr 使用 detach 解除绑定,对于已经添加的Fragment 使用 attach 重新加入。

前面我们知道,detach ,Activity 会保存维护 其状态,但是不能确保状态一定能够恢复。

使用情形:

如果Fragment 个数是有限的,那么推荐使用 FragmenStatetPagerAdapter,反之 使用 FragmentPagerAdapter。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,430评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,232评论 3 20
  • Fragment概述 Fragment是Activity中用户界面的一个行为或者说是一部分。主要是支持大屏幕上动态...
    wangling90阅读 11,577评论 5 75
  • 其时,我们都很累了,只是每个人都习惯假装坚强,习惯了自己一个人面对所有,不知道自己到底想怎么样。有时候可以很开心的...
    借过你挡光了阅读 167评论 0 0
  • 文:苏慕宁 初见时,他是跟着师兄下山去七秀坊按例讨香火钱的小和尚,她是正跟着师姐们练舞的小姑娘。他一入内坊门,就见...
    鸫雏阅读 453评论 0 0