Sobes.tech
Junior — Middle

Can you name and explain the main annotations used in Jetpack Compose?

sobes.tech AI

Answer from AI

In Jetpack Compose, the main annotations help manage state, composition, and UI optimization:

  • @Composable — marks a function as composable, meaning it describes UI and can be called within other composable functions.

  • @Preview — used to display a component preview in Android Studio without running the app.

  • @Stable — indicates that an object is stable and does not change, which helps optimize redraws.

  • @Immutable — signifies that an object is immutable, also affecting optimization.

  • @ReadOnlyComposable — marks a function that does not change state and can be called in certain contexts.

Example:

@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!")
}

@Preview
@Composable
fun PreviewGreeting() {
    Greeting("World")
}