加载片段撰写,将commit几次称为commit
我试图通过supportfragmentManager
来加载片段,如下所示。
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AndroidViewBinding(FragmentContainerBinding::inflate) {
supportFragmentManager.beginTransaction()
.replace(container.id, MyFragment()).commit()
}
}
}
}
但是,当显示该视图时,片段会多次犯下(加载)(即加载)(即几次调用increate()
)
任何方法都可以防止犯下几次? 是否有一种方法可以恢复国家(例如,如果被系统杀死,如何恢复它)?
(注意:我不使用androidx.fragment.app.fragmentContainerview
在xml中,如
I tried to load a Fragment in Compose as below, through the supportFragmentManager
as shown below.
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AndroidViewBinding(FragmentContainerBinding::inflate) {
supportFragmentManager.beginTransaction()
.replace(container.id, MyFragment()).commit()
}
}
}
}
However, when the view is shown, the fragment gets committed (loaded) several times (i.e. the onCreate()
is called several times)
Any way to prevent committing several times?
Is there a way to resume the state as well (e.g. in case got killed by the system, how to get it restored)?
(note: I'm not using the androidx.fragment.app.FragmentContainerView
in the XML as in Developer Doc I do have different fragments per some logic (not shown here), hence I'll have to use supportFragmentManager
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了获取此工作说明的方法,
这将需要Fragment的KTX
Found a way to get this working
Note this will need Fragment's KTX