Search code examples
androidkotlinandroid-jetpack-composeandroid-fragmentactivityandroid-dialogfragment

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()
    }
}
}

Solution

  • 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()
        }
    }
    }