Android jetpack what is setContent()?
setContent()
is a function provided by the Android Jetpack Compose framework to set the content of a Compose-based screen in an Android app. It is used to declare the UI of a Compose screen, and it takes a lambda expression that contains the Compose UI code.
The setContent()
function is typically used in the onCreate()
or onViewCreated()
method of an activity or fragment to specify the content of that screen. Here's an example:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text("Hello, world!")
}
}
}
In this example, the setContent()
function is called with a lambda expression that contains a single Text
composable. This composable displays the text "Hello, world!" on the screen.
The setContent()
function is one of the fundamental building blocks of the Compose framework, and it allows you to create sophisticated UIs by composing simple building blocks, or composables, together.