【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()
}
相关推荐
非专业程序员Ping6 小时前
HarfBuzz概览
android·ios·swift·font
Jeled7 小时前
「高级 Android 架构师成长路线」的第 1 阶段 —— 强化体系与架构思维(Clean Architecture 实战)
android·kotlin·android studio·1024程序员节
明道源码9 小时前
Kotlin 控制流、函数、Lambda、高阶函数
android·开发语言·kotlin
消失的旧时光-194311 小时前
Kotlin × Gson:为什么遍历 JsonObject 要用 entrySet()
android·kotlin·数据处理·1024程序员节
G果12 小时前
安卓APP页面之间传参(Android studio 开发)
android·java·android studio
曾凡宇先生13 小时前
无法远程连接 MySQL
android·开发语言·数据库·sql·tcp/ip·mysql·adb
zhangphil15 小时前
Android GPU的RenderThread Texture upload上传Bitmap优化prepareToDraw
android
柿蒂16 小时前
聊聊SliverPersistentHeader优先消费滑动的设计
android·flutter
假装多好12318 小时前
android三方调试几个常用命令
android·1024程序员节·三方,gms
侧耳42918 小时前
android11禁止安装apk
android·java·1024程序员节