Call jetpack compose from companion object with context

814 Views Asked by At

I want to try Jetpack Compose and I encountered with issues.

Can I call composable from companion object?

companion object {
  fun showDialog(context: Context){
    ComposeView(context = context).apply {
        setContent { //isAttachedToWindow = false
           CustomDialog(progressIndicator = false) //custom dialog with @Composable
        }
    }
  }
}    

And additional question: Should I need ModelView class for logic which create data for this CustomDialog and catch callback from this dialog?  
1

There are 1 best solutions below

0
user21948199 On

You can create a companion class, then call it inside your composable function. For example:

@Composable
fun MyComposableFunction() {
   Text(text = CompanionClass.MY_TEXT)
}

class CompanionClass() {
   companion object {
      const val MY_TEXT = "some text"
   }
}

Hope this helps you out!