Fragment 知识巩固

重新学习 Fragment

1.Fragment 的生命周期

想要熟练使用 Fragment,那么必须要弄懂它的生命周期。

我们可以先看一下 Fragment 生命周期和 Activity 生命周期的关联

 

 

该图展示了Fragment 和 Actvity 两者的生命周期的关系,我们可以看到 Fragment 比 Activity 多了一些生命周期的回调方法。

 

下面详细说明 Fragment 各个生命周期的作用:

1.onAttach(Context):

    当 Fragment 和 Activity 发生关联时调用该方法。执行该方法时,Fragment 和 Activity 已经完成了绑定,该方法中的 Context 参数就代表已绑定的 Activty,你可以通过该方法获取到宿主 Activity 。

2.onCreate():

    初始化 Fragment ,可通过参数 savedInstanceState 获取到保存的 Fragment 状态或者自己保存的数据。

3.onCreateView(LayoutInflater,ViewGroup,Bundle):

    初始化 Fragment 的布局,一般在该方法中进行加载布局的操作,返回布局 view 到 OnViewCreated()方法中该方法中最好不要执行耗时操作,比如读取数据库数据等。

4.onViewCreated(View,Bnudle):

    在 onCreateView()执行后调用,获得其返回的 view ,表示布局 view 已加载完成。一般可在该方法中进行 findViewById 的操作。

5.onActivityCreate(Bundle):

    当关联的 Activity 的 onCreate 方法返回时调用,所以在该方法内可以进行与 Activity 交互的 UI 操作。(注意:在该方法之前由于 Activity 的onCreate 方法还未执行,如果提前进行交互操作,会引发空指针异常),一般可在该方法中加载数据。

6.onStart():

    在 Fragment 可见时调用,类似 activity 的 onStart()方法。

7.onResume():

    在 Fragment 处于活动状态时调用,用户可与之进行 UI 交互,类似 activity 的 onResume()方法。

8.onPause():

    在 Fragment 处于暂停状态时调用,此时 fragment 仍可能可见,但与用户不能进行交互,类似 activity 的 onPause()方法。

9.onStop():

    在 Fragment 由可见切换到不可见状态时调用,类似 activity 的 onStop()方法。

10.onDestoryView():

    与 onCreateView 对应,当 fragment 的视图被销毁时调用,不过此时 Fragment 与 Activity 并未解除绑定,仍可通过 onCreateView()创建视图。

11.onDestory():

    销毁 Fragment。

12.onDetach():

    解除与 Activity 的关联,在 onDestroy() 方法之后调用。

 

 下图更详细的展现了Fragment与Activity的生命周期的调用顺序:

 

2.Fragment 的使用

想要在 Activity 中添加 Fragment,既可以使用静态添加,也可以使用动态添加,两者的区别是,静态添加的 Fragment 固定不可切换,而动态添加方法可以随意切换不同的 Fragment。

      先说静态添加 Fragment ,静态添加 Fragment 的方法使用起来很简单,我们只需要在在 Activity 的布局文件中添加一个 Fragment 控件,然后通过 android:name 属性来确定具体的 Fragment 即可。具体写法如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/sample_list_fragment"
        android:name="com.example.projectmodule.fragment.SampleFragment"   //该行确定要显示的Fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

      再说动态添加 Fragment,一般我们会使用一个 FrameLayout 作为容器来装载 Fragment ,当切换不同的 Fragment 时,变化的只是 FrameLayout 的区域。xml 文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
</LinearLayout>

 

      我们如果想要动态的切换 Fragment ,那么就必须要使用到 FragmentManager(碎片管理者)和 FragmentTransaction(碎片事务)。想要获取 FragmentManager,通过 getFragmentManager() 方法即可(如果使用的是V4包下的 Fragmnet,则使用 getSupportFragmentManager() 方法)。然后我们可以通过 FragmentManger 来开启一个事务。

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  //获得一个FragmentManager,并开启一个事务

通过使用 FragmnetTransaction 中不同的方法可以对 Fragment 进行不同的操作,主要方法如下:

1、transaction.add():

      将一个碎片添加到 FragmentManager 中,第一个参数填你的 containerID ,第二个参数填你要放入的 Fragment

2、transaction.remove():

      将一个碎片从 FragmentManager 中移除,一般情况下,移除的 Fragment 会被销毁。

3、transaction.hide():

      隐藏 Fragment ,该方法并不会销毁 Fragmnet ,只是会将 Fragment 设为不可见。

4、transaction.show():

      将隐藏的 Fragment 变为可见状态。

5、transaction.replace():

      将正在显示的 Fragment 从 FragmnetManager 中移除,并且添加新的 Fragment 来替换它。(相当于调用了 remove() 方法后有调用了 add() 方法)

6、transaction.detach():

      只销毁该 Fragment 的视图,并不完全销毁 Fragment(即执行了 onDestoryView() 方法,但未执行 onDestory() 方法),该 Fragment 仍然在FragmentManager 中。

7、transaction.attach():

      恢复 Fragment 的视图(执行了 onCreateView() 方法)。

 

      最后不要忘记,无论是调用了哪些方法,transaction 最后都要调用一下 commit() 方法,把事务给提交上去,否则前面的操作都不会被执行。

      如果你对 FragmentPagerAdapter 和 FragmentStatePagerAdapter 比较熟悉的话,根据上述方法的介绍,就可以推论出 FragmentPagerAdapter 中移除 Fragment 时使用的是 detach() 方法,而FragmentStatePagerAdapter 中移除 Fragment 时使用的时 remove() 方法。查看源码可以发现确实是这样做的。

 

Fragment的通信

      Fragment 的通信主要分三种情况:Fragment 与 Activity 的通信,Activity 与 Fragment 的通信,Fragment 与 Fragment 的通信。

      Fragment 与 Activity 的通信指 Fragment 如何调用 Activity 中的方法,其实我们只要在 Fragment 中的 onAttach() 之后调用 getActivity() 方法就可以得到 Fragment 的宿主 Activity 了,然后就可以调用 Activity 中的方法。不过记得在 Fragment 与 Activity 解除关联时将获得的 Activity 给释放,以免造成内存泄漏。

      Activity 与 Fragment 通信指在 Activity 中调用 Fragment 的方法,这个我们一般通过回调方法来实现,我们可以在 Fragment 中声明一个接口,在 Activity 中来实现接口,以达到 Activity 调用 Fragment 中的方法。

      Fragment 与 Fragment 的通信,实际上是使用了 Activity 作为中转站。我们先在一个 Fragment 中通过 getActivity() 方法得到宿主 Activity ,然后再调用 Activity 的findFragmentById() 方法或 findFragmentByTag() 方法得到另一个 Fragment,然后调用它的方法,从而实现两个 Fragment 之间的通信。

posted @ 2017-09-10 10:25  微啊微末  阅读(264)  评论(0编辑  收藏  举报