【Android】动态添加 Fragment

(1) 创建待添加Fragment 的实例。

(2) 获取FragmentManager,在Activity 中可以直接调用getSupportFragmentManager()

方法获取。

(3) 开启一个事务 ,通过调用beginTransaction()方法开启。

(4) 向容器内添加或替换Fragment ,一般使用replace()方法实现,需要传入容器的id和待添加的Fragment 实例。

(5) 提交事务 ,调用commit()方法来完成。

kt 复制代码
moPubListFragment = MoPubListFragment().also {
    it.arguments = intent.extras
    supportFragmentManager.beginTransaction()
        .replace(
            R.id.fragment_container,
            it,
            LIST_FRAGMENT_TAG
        ).commit()
}
相关推荐
quanyechacsdn10 小时前
Android Studio创建库文件用jitpack构建后使用implementation方式引用
android·ide·kotlin·android studio·implementation·android 库文件·使用jitpack
程序员陆业聪11 小时前
聊聊2026年Android开发会是什么样
android
编程大师哥11 小时前
Android分层
android
极客小云13 小时前
【深入理解 Android 中的 build.gradle 文件】
android·安卓·安全架构·安全性测试
Juskey iii13 小时前
Android Studio Electric Eel | 2022.1.1 Patch 2 版本下载
android·ide·android studio
Android技术之家13 小时前
2025年度Android行业总结:AI驱动生态重构,跨端融合开启新篇
android·人工智能·重构
洞见前行13 小时前
Android第二代加固技术原理详解(附源码)
android
风清云淡_A13 小时前
【JetCompose】入门教程实战基础案例01之显隐动画
android
2501_9160074713 小时前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063213 小时前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview