Android Activity+Fragment切换方式及方法互调

简单记录一下

1.在宿主Activity中

//第一个fragment
private var sceneCheckFragment: SceneCheckFragment? = null
//第二个fragment
private var recordSystemFragment: RecordSystemFragment? = null

private val sceneCheckFragmentTag = "SceneCheckFragment"
private val recordSystemFragmentTag = "RecordSystemFragment"

开启事务初始化要加载的fragment,并且隐藏另一个

    /**
     * 显示 现场检查 fragment
     */
    fun initSceneCheckFragment() {
        isShowSceneCheckFragment = true
        //开启事务,fragment的控制是由事务来实现的
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        //add,初始化fragment并添加到事务中,如果为null就new一个
        if (sceneCheckFragment == null) {
            sceneCheckFragment = SceneCheckFragment()
            transaction.add(R.id.fragment_container, sceneCheckFragment!!, sceneCheckFragmentTag)
        }
        hideFragm

你可能感兴趣的:(Android,android,android,studio,kotlin)