【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()
}
相关推荐
沐怡旸18 分钟前
【底层机制】Android OTA更新系统:原理与应用深度解析
android·面试
q***31141 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
android·前端·后端
SkyQvQ2 小时前
Android Studio 开发效率神器:Auto-import
android·android studio
木子予彤2 小时前
Compose 中的系统区域适配
android·android jetpack
q***72193 小时前
Y20030018基于Java+Springboot+mysql+jsp+layui的家政服务系统的设计与实现 源代码 文档
android·前端·后端
Code Warrior3 小时前
【MySQL数据库】数据类型
android·数据库·mysql
a***13145 小时前
python的sql解析库-sqlparse
android·前端·后端
r***86985 小时前
mysql的主从配置
android·mysql·adb
.豆鲨包5 小时前
【Android】深入理解Activity的生命周期和IntentFilter
android·java
啃火龙果的兔子5 小时前
安卓从零开始
android