Android开发: Fragment最全面使用详解

前言

Fragment在Android开发中非常常用,今天就来介绍下Fragment及其使用方法

目录

Fragment介绍&使用方法解析.png

定义

Fragment是activity的界面中的一部分或一种行为

1.把Fragment认为模块化的一段activity
2.它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除
3.Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的所有的Fragment们都暂停了,当activity销毁时,它拥有的所有Fragment们都被销毁。

作用

主要是为了支持更动态、更灵活的界面设计(从3.0开始引入)

Fragment的生命周期解析

先来看张官方说明图

详细解读每个方法的调用场景

  • onAttach方法
    Fragment和Activity建立关联的时候调用(获得activity的传递的值)
  • onCreateView方法
    为Fragment创建视图(加载布局)时调用(给当前的fragment绘制UI布局,可以使用线程更新UI)
  • onActivityCreated方法
    当Activity中的onCreate方法执行完后调用(表示activity执行oncreate方法完成了的时候会调用此方法)
  • onDestroyView方法
    Fragment中的布局被移除时调用(表示fragment销毁相关联的UI布局)
  • onDetach方法
    Fragment和Activity解除关联的时候调用(脱离activity)

fragment生命周期解析

  • 当一个fragment被创建的时候:
    onAttach()
    onCreate()
    onCreateView()
    onActivityCreated()

  • 当这个fragment对用户可见的时候,它会经历以下状态。
    onStart()
    onResume()

    1.2可以理解为从创建到显示(或切换)

  • 当这个fragment进入“后台模式”的时候,它会经历以下状态。
    onPause()
    onStop()

  • 当这个fragment被销毁了(或者持有它的activity被销毁了):
    onPause()
    onStop()
    onDestroyView()
    onDestroy()
    onDetach()

  • 就像Activity一样,在以下的状态中,可以使用Bundle对象保存一个fragment的对象。
    onCreate()
    onCreateView()
    onActivityCreated()

其他场景的调用

  • 屏幕灭掉
    onPause() onSaveInstanceState() onStop()

  • 屏幕解锁
    onStart() onResume()

  • 切换到其他Fragment
    onPause() onStop() onDestroyView()

  • 切换回本身的Fragment
    onCreateView() onActivityCreated() onStart() onResume()

  • 回到桌面
    onPause() onSaveInstanceState() onStop()

  • 回到应用
    onStart() onResume()

  • 退出应用
    onPause() onStop() onDestroyView() onDestroy() onDetach()

Fragment和Activity的生命周期很相似,以下是对比图

Fragment的使用

由于Fragment作为Activity的一部分,所以Fragment的使用一般是添加到Activity中,一般有两种方法将Fragment添加到Activity中:

  • 方法1:在activity的layout.xml文件中声明fragment
  • 方法2:在activity的layout.xml文件中声明包含

方法1:在activity的layout.xml文件中声明fragment

  • layout文件fragment_layout_test.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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" >
<fragment android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

说明

  1. 该layout只包含了一个fragment元素。
  2. fragment的属性
    1
    android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment"

说明该fragment是定义在包名为”com.skywang.app”中类”FragmentLayoutTest””的内部类”ExampleFragment”中的。

  • FragmentLayoutTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.skywang.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.app.Fragment;
public class FragmentLayoutTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fragment_layout_test只包括ExampleFragment对象。
setContentView(R.layout.fragment_layout_test);
}
// 继承与Fragment
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
}

说明:

  1. FragmentLayoutTest通过

    1
    setContentView(R.layout.fragment_layout_test)

    来调用fragment_layout_test.xml来作为布局文件。

  2. 上面的fragment_layout_test.xml布局文件仅仅值包括1个Fragment,该Fragment是通过FragmentLayoutTest的内部类ExampleFragment实现的
  3. 内部类ExampleFragment继承于Fragment,在onCreateView()方法中通过

    1
    return inflater.inflate(R.layout.example_fragment, container, false)

    来将example_fragment.xml作为其布局文件。
    所以,相当于FragmentLayoutTest直接调用example_fragment.xml来显示。

那为什么要费这么大劲,非要用到Fragment呢?原因是因为Fragment的可扩展性,它能作为一个独立的显示单元添加到activity中。本文仅仅只是为了说明fragment的layout实现方法,实际应用中,可能比这复杂很多。

  • example_fragment.xml
1
2
3
4
5
6
7
8
9
10
11
12
<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" >
<TextView
android:text="@string/example_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

方法2:在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment

该Fragment采用动态加载的方式。

  • FragmentTransactionTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.skywang.app;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class FragmentTransactionTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transaction_test);
// 获取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 获取ExampleFragment
ExampleFragment fragment = new ExampleFragment();
// 将fragment添加到容器about_fragment_container中
fragmentTransaction.add(R.id.about_fragment_container, fragment);
fragmentTransaction.commit();
}
// 继承与Fragment
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
}

说明

  1. 在onCreate()中该activit调用fragment_transaction_test作为其布局文件
  2. 在onCreate()中,通过获取FragmentManager和FragmentTransaction,来将ExampleFragment对象添加到R.id.about_fragment_container中,about_fragment_container是一个Fragment。
  3. ExampleFragment的onCreateView()中,将example_fragment作为其布局文件。
  • fragment_transaction_test.xml
1
2
3
4
5
6
7
8
9
10
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/about_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
  • example_fragment.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<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" >
<TextView
android:text="Hello Fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

总结

本文对Fragment进行了全面介绍和分析,接下来我会介绍继续介绍Android开发中的相关知识,有兴趣可以继续关注Carson_Ho的安卓开发笔记


欢迎关注Carson_Ho的简书!

不定期分享关于安卓开发的干货,追求短、平、快,但却不缺深度

文章目录
  1. 1. 前言
  2. 2. 目录
  3. 3. 定义
  4. 4. 作用
  5. 5. Fragment的生命周期解析
    1. 5.1. 先来看张官方说明图
      1. 5.1.1. 详细解读每个方法的调用场景
      2. 5.1.2. fragment生命周期解析
      3. 5.1.3. 其他场景的调用
      4. 5.1.4. Fragment和Activity的生命周期很相似,以下是对比图
  6. 6. Fragment的使用
    1. 6.0.1. 方法1:在activity的layout.xml文件中声明fragment
  • 7. 方法2:在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment
    1. 7.0.1. 总结
    2. 7.0.2. 欢迎关注Carson_Ho的简书!
  • ,