I am teaching myself Android Jetpack Compose and I am trying to understand something on Composable Functions Calling.
The Official Android Doc states that "Composable functions can only be called from within the scope of other composable functions".
I have this code that calls Greeting
Composable fxn inside the setContent
Block.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
//calling Greeting() inside the setContent() block
Greeting("Me")
}
}
}
//Composable function
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!", modifier = Modifier.padding(16.dp))
}
Does this then make setContent Block a Composable since we are calling a Composable function inside it?
Please let me have your thoughts and comments, thanks guys.
In your Activity
, to create a Compose-based screen, you have to call the setContent()
method, and pass whatever composable functions you like.
You can check the source code:
public fun ComponentActivity.setContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit
)
where content
is A @Composable
function declaring the UI contents.