Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is setContent Block a Composable?

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.

like image 666
Tonnie Avatar asked Sep 15 '25 21:09

Tonnie


1 Answers

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.

like image 80
Gabriele Mariotti Avatar answered Sep 18 '25 17:09

Gabriele Mariotti