4

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.

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this insight, I see the relationship now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.