美文网首页
Fragment 的深入思考

Fragment 的深入思考

作者: 媚竹风 | 来源:发表于2017-11-08 09:41 被阅读14次

    概述

    这篇博客,是关于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。

    相关文章

      网友评论

          本文标题:Fragment 的深入思考

          本文链接:https://www.haomeiwen.com/subject/eslemxtx.html