第三天笔记

Fragment 重点知识总结

image-20240708081704464

image-20240708081754080

image-20240708083144092

image-20240708083534184

1. fragment背景

Fragment的诞生

  • 引入版本: Android 3.0 (API 11)

  • 背景和目的:

  • 初衷是为了适应大屏幕的平板电脑,由于平板电脑的屏幕比手机屏幕更大,因此可以容纳更多的UI组件,且这些UI组件之间存在交互关系。

    • 使用Fragment对UI组件进行分组、模块化管理,就可以更方便地在运行过程中动态更新Activity的用户界面。
  • 作为一个微型Activity而诞生,迈出了缩减Activity之路上的一步。

2. 创建fragment

添加Fragment依赖

  • Fragment需要依赖于AndroidX Fragment库:
    • 添加Google Maven代码库,并在build.gradle中添加此依赖项。
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
    }
}
dependencies {
    def fragment_version = "1.5.5"
    // Java language implementation
    implementation "androidx.fragment:fragment:$fragment_version"
    // Kotlin
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
}

查看gradle版本依赖:

./gradlew app:dependencies

创建Fragment

  • res/layout文件夹下,创建example_fragment.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment View"/>
</LinearLayout>
  • 创建ExampleFragment类,继承Fragment类,并向基本构造函数提供Fragment的布局资源:
class ExampleFragment extends Fragment {
    public ExampleFragment() {
        super(R.layout.example_fragment);
    }
}

image-20240708084954485

image-20240708085553077

image-20240708092419239

Fragment 事务操作

add 和 replace 操作

  • add:
    • 不会删除容器内已存在的Fragment
    • 原Fragment生命周期不受影响
    • 原Fragment会被保存和自动恢复
    • 内存占用多
    • 搭配hide/show使用
  • replace:
    • 删除容器内已存在的Fragment
    • 原Fragment被销毁
    • 原Fragment状态无法恢复
    • 内存占用少

事务commit的几种方式

方法 说明
commit 非立即执行,将消息发送到主线程队列等待执行。需要立即执行时配合executePendingTransactions将队列中所有事务全部执行。
commitNow 立刻仅执行当前事务,不能与addToBackStack同时使用。
commitAllowingStateLoss onDestroy时提交不再抛出IllegalStateException,而是丢弃提交的Fragment。
commitNowAllowingStateLoss 立刻执行并允许状态丢失。

image-20240708095745805

image-20240708103753459

image-20240708103841833

image-20240708103919238

image-20240708104818847

<!--        android:label="@string/app_name"-->

image-20240708111312149

image-20240708111638293

image-20240708111950014

image-20240708112204044

image-20240708112214073

image-20240708112331604

image-20240708112416821

image-20240708112648568

随堂练习

目标: 掌握Fragment生命周期的执行过程

Todo List:

  • 在MainActivity、DemoFragment和DemoFragment2生命周期各方法中添加日志
  • 分析启动并添加DemoFragment时的生命周期日志
  • 分析按下Home键后的生命周期日志
  • 分析使用add与replace显示DemoFragment2时生命周期日志差异
  • 分析按下返回键后的生命周期日志

image-20240708135228099

image-20240708135702109

Fragment通信

  • 每个Fragment应为完全独立的模块化组件,定义它自己的布局和行为
  • 两个Fragment不能直接通信

通信方式

  • 与Activity通信

  • 外部对外public方法

  • Fragment Result API

image-20240708144902199

image-20240708155255045

image-20240708165312688

image-20240708165631567

Fragment动画

image-20240708165747050

image-20240708165808726

image-20240708165902025

image-20240708165952627

image-20240708170018771

可以使用按钮实现

重点Activity BroadCastReceiver Fragment