在Compose Activity中显示DialogFragment

3

我有一个扩展了ComponentActivity的Activity(用于基于Compose实现的Activity变体),因此无法访问FragmentManager。

是否有办法在其中显示使用View System实现的DialogFragment?

class MyActivity : ComponentActivity(){

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
    //how to show DialogFragment here? there is no access to FragmentManager
        ScreenContent()
    }
}
}

Compose 中有一个 Context 可用,但无法访问 FragmentManager(或其更新的变体 SupportFragmentManager)。 - Mohammad cs
这个回答是否解决了你的问题?使用Activity可以获取FragmentManager。 - Phil Dukhov
不,这不是我的问题的答案,我想要使用supportFragmentManager显示片段。 - Mohammad cs
1个回答

3
我认为,你应该使用FragmentActivity而不是ComponentActivity。它已经从ComponentActivity继承并支持片段管理器。我已经使用过它并且它运行良好。
例如;
class MyActivity : FragmentActivity(){

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        val dialogFragment = CustomDialogFragment.newInstance() // DialogFragment
        dialogFragment.show(supportFragmentManager, "Your classname")
        ScreenContent()
    }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接