【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()
}
相关推荐
yeziyfx12 小时前
kotlin中 ?:的用法
android·开发语言·kotlin
2501_9159184114 小时前
只有 Flutter IPA 文件,通过多工具组合完成有效混淆与保护
android·flutter·ios·小程序·uni-app·iphone·webview
robotx14 小时前
AOSP 设置-提示音和振动 添加一个带有开关(Switch)的设置项
android
青莲84314 小时前
RecyclerView 完全指南
android·前端·面试
青莲84314 小时前
Android WebView 混合开发完整指南
android·前端·面试
龙之叶14 小时前
【Android Monkey源码解析三】- 运行解析
android
KevinWang_16 小时前
Android 的 assets 资源和 raw 资源有什么区别?
android
码农幻想梦16 小时前
2021Android从零入门到实战(慕课网官方账号)
android
Jomurphys17 小时前
Android 架构 - 组件化 Modularzation
android
明明明h17 小时前
【Unity3D】Android App Bundle(aab)打包上架Google Play介绍
android