【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_9159214310 分钟前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
June bug2 小时前
【配环境】安卓项目开发环境
android
2501_944526424 小时前
Flutter for OpenHarmony 万能游戏库App实战 - 蜘蛛纸牌游戏实现
android·java·python·flutter·游戏
csj504 小时前
安卓基础之《(18)—内容提供者(4)在应用之间共享文件》
android
尤老师FPGA5 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十五讲)
android·java·ui
北辰当尹6 小时前
xml基础
android·xml
龙之叶6 小时前
【Android Monkey源码解析四】- 异常捕获/页面控制
android·windows·adb·monkey
_F_y7 小时前
MySQL表的操作
android·数据库·mysql
yngsqq8 小时前
AndroidStudio汉化步骤
android
HyEISN9 小时前
Android 9 开启远程adb
android·adb