gpt4 book ai didi

android - 我在父fragment1中有子Fragment2,我想在单击后更改 fragment ,而在下次按下时要更改 fragment ,我想关闭 Activity 吗?

转载 作者:行者123 更新时间:2023-12-02 13:44:45 25 4
gpt4 key购买 nike

用于更改子fragment2(fragment 1父片段中的容器)

       view.isFocusableInTouchMode = true
view.requestFocus()
view.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
childFragmentManager!!.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE)

return@OnKeyListener true
}

false
})

结束 Activity
override fun onBackPressed() {
if (doubleBackToExitOnce) {
super.onBackPressed()
finishAffinity()
return
}

this.doubleBackToExitOnce = true

Toast.makeText(applicationContext, "Press again to exit ", Toast.LENGTH_SHORT).show()

Handler().postDelayed({
kotlin.run { doubleBackToExitOnce = false }
}, 2000)
}

在这种情况下,我只能更改子片段,而无法关闭 Activity

最佳答案

getActivity()
.getSupportFragmentManager()
.beginTransaction()
.remove(getSupportFragmentManager().findFragmentById(R.id.container)).commit();

你也可以通过TAG删除片段

关于android - 我在父fragment1中有子Fragment2,我想在单击后更改 fragment ,而在下次按下时要更改 fragment ,我想关闭 Activity 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59858581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com