https://kotlinlang.org logo
#compose
Title
# compose
w

wintersoldier

01/26/2023, 1:06 AM
Hi , I'm working on compose interoperability with view system. I have a usecase where I have to pass the composable as content to a custom view which I created which internally calls a composable. Attaching the code for reference.
Copy code
@Composable
fun Test(
  content: @Composable () -> Unit
) {
  Column {
    content()
  }
}

class TestView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {

  var content by mutableStateOf<Composable?>(null)

  @Composable
  override fun Content() {
    content?.let {
      Test(
        content = {
          it
        },
      ) 
    }
  }
}
k

Kirill Grouchnikov

01/26/2023, 12:39 PM
There's no question here. Is the code not compiling? Crashing at runtime? Partially displaying? Not displaying anything? Displaying the view but not allowing to interact with it? Something else?
w

wintersoldier

01/26/2023, 12:42 PM
My question is , there is content which is var content by mutableStateOf<Composable?>(null) So from an activity which uses view system , how can I set a view to this content object
a

Andy Himberger

01/27/2023, 5:30 PM
Who sets it / it needs to dynamically change? Probably the 'compose' way to do this would be to use navigation and the thing wanting to change content would navigate to the right composable, but you need to define all the possible destinations. We have a couple test apps not using navigation yet doing something similar to what you have, except we don't store a Composable in state, we have an enum class where one of the values is a Composable, and we store the enum in state, that seems to work.
4 Views