androidx.activity.ComponentActivity#ComponentActivity()
As we all know, non-compose needs to inherit AppCompatActivity to create an Activity
They eventually inherit as follows:
ComponentActivity: ComponentActivity, ComponentActivity, ComponentActivity, ComponentActivity, ComponentActivity, ComponentActivity
Base classes that combine higher-level component activities, not all functionality is built directly into this class, reducing coupling
Compose inherits ComponentActivity: Compose inherits ComponentActivity: Compose inherits ComponentActivity Where to create and load Componse UI, how does not seem to find ๐ , please everyone center eyeglasses frame, look up, just ๐ComponentActivity official explanation ๐ I this violent temper, you this is not fart to the air? How do you create the Compose UI?
At this point you need to use the ComposeView passed into the setContentView(View), ๐๐ do you feel reasonable?
setContentView(
ComposeView(this).apply { setContent { ........ }})Copy the code
Let’s see what’s going on in the ComposeView?
The nested ๐ pseudocode is as follows:
ComposeView(this){
setContent{
AbstractComposeView#createComposition(){
ViewGroup.setContent(){
// Create AndroidComposeView and pass in doSetContent(...)
//AndroidComposeView inherits from ViewGroup
ViewGroup.doSetContent(){
WrappedComposition#setContent(){
owner.setOnViewTreeOwnersAvailable {
CompositionLocalProvider(...) {
ProvideAndroidCompositionLocals(owner, content)
}
}
}
}
}
}
}
}
Copy the code
The actual conversion of functions into concrete interfaces is done in AndroidComposeView
OnMeasure, onLayout, dispatchDraw
onMeasure
onLayout
dispatchDraw
The ๐ creation process is pretty much the same as the one above, ending up in ViewGroup mode, except that we don’t feel as much ๐๐ in the Compose UI framework
For example, if you want to create an onCreate ComponseView for your Activity, you can use the following lib libraries to create an extension function for the Activity
implementation 'androidx. Activity: activity - compose: the latest version number'
Copy the code
After relying on the lib library above, you can use the extension library below
fun ComponentActivity.setContent(
parent: CompositionContext? = null.content: @Composable () -> Unit
)
Copy the code
The final version is as follows:
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContent {
// Pass in the Compose UI decorated with @composable to display the content}}Copy the code
The article is very short, there is no dry goods to talk about. The so-called master leads the door and cultivates himself. Please eat slowly and leave without giving a thumbs up.