MainActivity extends androidx.activity.ComponentActivity, the smallest base class that is a LifecycleOwner, ViewModelStoreOwner and SavedStateRegistryOwner: that is how viewModel() and rememberSaveable find their owners. AppCompatActivity extends it and is needed only for Fragments or AppCompat themes. setContent { } creates a ComposeView, makes it the window's content and starts composition, replacing setContentView(R.layout.main). enableEdgeToEdge(), called first, makes the system bars transparent and draws content behind them; apps targeting API 35 or later get that on Android 15 and up anyway, so the call mainly aligns older versions. Scaffold passes the bar insets to its content, which must apply them:
@Composable
fun BookDetail() {
var inCart by rememberSaveable { mutableIntStateOf(0) } // written to the saved Bundle
var taps by remember { mutableIntStateOf(0) } // lives only in this instance
val landscape = LocalConfiguration.current.orientation == ORIENTATION_LANDSCAPE
Scaffold { insets -> // insets = system bar padding
Column(Modifier.padding(insets).padding(24.dp), Arrangement.spacedBy(12.dp)) {
Text("Patterns of the Deep Web", style = MaterialTheme.typography.headlineSmall)
Text("Tomas Reyes, $39.50 (" + (if (landscape) "landscape" else "portrait") + ")")
Button(onClick = { inCart++; taps++ }) { Text("Add to cart") }
Text("In cart (rememberSaveable): $inCart")
Text("Taps (remember): $taps")
}
}
}Each tap on Add to cart raises both counters; Process Death and Configuration Changes show which one survives.