amar_1995
amar_1995

Reputation: 595

Methods to set content view in Jetpack Compose UI

I had three question

  1. There is setViewContent which accept composable function as input parameter similar to setContent. So, what is a difference between setViewContent and setContent and its use-cases. You can able to see setViewContent in androidx.compose package.
  2. setContent and setViewContent both return CompositionContext?. So, how and for what, we will use CompositionContent.
  3. Is there any way to integrate existing layout.xml with new compose ui in same activity or fragment.

Upvotes: 6

Views: 3310

Answers (1)

nglauber
nglauber

Reputation: 24044

Here are my comments and my understanding:

  1. setContent will make the composable passed as parameter as the root component of your activity/fragment. On the other hand, setViewContent will add a FrameLayout as root element of your activity/fragment which allow you to add another views on it.
  2. Both methods return a Composition object, which afaik, it's used just to display the content via setContent and clear the hierarchy that was created from the composition via dispose.
  3. Yes, in dev14 you can use AndroidView like this:
AndroidView(resId = R.layout.my_layout) { view ->
    val textView = view.findViewById<TextView>(R.id.textView)
    ...    
}

Upvotes: 2

Related Questions