【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()
}
相关推荐
xvch2 小时前
Kotlin 2.1.0 入门教程(二十五)类型擦除
android·kotlin
simplepeng10 小时前
我的天,我真是和androidx的字体加载杠上了
android
小猫猫猫◍˃ᵕ˂◍12 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
CYRUS_STUDIO14 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
梦否14 小时前
【Android】类加载器&热修复-随记
android
徒步青云14 小时前
Java内存模型
android
今阳14 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
-优势在我19 小时前
Android TabLayout 实现随意控制item之间的间距
android·java·ui
hedalei19 小时前
android13修改系统Launcher不跟随重力感应旋转
android·launcher
Indoraptor20 小时前
Android Fence 同步框架
android