Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show DialogFragment inside Compose Activity

I have an Activity that extends ComponentActivity (Activity variant that is used for Compose based Activity Implementation), thus have no access to FragmentManager.

Is there any way to show DialogFragment(implemented with View System) in it ?

class MyActivity : ComponentActivity(){

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
    //how to show DialogFragment here? there is no access to FragmentManager
        ScreenContent()
    }
}
}
like image 746
Mohammad cs Avatar asked Sep 01 '25 11:09

Mohammad cs


1 Answers

I reckon, you must use FragmentActivity instead of ComponentActivity. It is already extended from ComponentActivity and supported for fragment manager. I have used it and it worked.

For instance;

class MyActivity : FragmentActivity(){

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        val dialogFragment = CustomDialogFragment.newInstance() // DialogFragment
        dialogFragment.show(supportFragmentManager, "Your classname")
        ScreenContent()
    }
}
}
like image 112
Hacı Abdullah Sarıkaya Avatar answered Sep 03 '25 00:09

Hacı Abdullah Sarıkaya