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

fengdai

10/28/2019, 8:36 AM
Does anyone know the difference between
ViewGroup.setContent
(in Wrapper) and
ViewGroup.setViewContent
(in Compose)?
l

Luca Nicoletti

10/28/2019, 9:57 AM
Well, they internally call a different implementation of
Compose.composeInto
one of them returns a nullable
CompositionContext
the other instead does not allows nullable
f

fengdai

10/28/2019, 10:04 AM
Sorry, I meant the difference between the purpose of them.
a

Andrey Kulikov

10/28/2019, 11:24 AM
setContent is adding our AndroidComposeView which allows using Compose UI composables as a content. setViewContent is not really should be used for now, but this allows using compose runtime with just Android plain Views, see the example: https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/RippleActivity.kt. Note that this mode is not fully tested and it is not yet decided afaik in what shape it will be supported in funute. We would probably use it for compatibility to allow using some Views inside Compose UI composables
f

fengdai

10/28/2019, 11:53 AM
@Andrey Kulikov Thanks for your explanation. I tried
setViewContent
, but the code can not be complied:
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewContent {
            TextView(this)
        }
    }
}
a

Andrey Kulikov

10/28/2019, 12:02 PM
as you can see in my example you don't need to pass context. so if you remove
this
and instead add something like
text = "Test"
it should compile
f

fengdai

10/28/2019, 12:10 PM
It works! AS marks the construction statement as an error, but it can be compiled. Seams that something magical happened. Cool. 😝
a

Andrey Kulikov

10/28/2019, 12:15 PM
yeah, that what I meant by "not really should be used"
f

fengdai

10/28/2019, 12:20 PM
I see. Thanks again.
2 Views