【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()
}
相关推荐
2501_937189237 小时前
2025 优化版神马影视 8.8 源码系统|零基础部署
android·源码·开源软件·源代码管理·机顶盒
モンキー・D・小菜鸡儿9 小时前
Android Jetpack Compose 基础控件介绍
android·kotlin·android jetpack·compose
无风之翼10 小时前
android15 休眠唤醒过程中有时候屏幕显示时间一闪而过
android·锁屏
方白羽11 小时前
Android全局悬浮拖拽视图
android·app·客户端
Jerry13 小时前
Compose 高级状态和附带效应
android
2501_9160074714 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
LFly_ice14 小时前
Nest-管道
android·java·数据库
ab_dg_dp16 小时前
android bugreport 模块源码分析
android
2501_9151063217 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
繁星星繁17 小时前
【Mysql】数据库基础
android·数据库·mysql