import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
@Composable
fun RememberSaveableDemo() {
var count by rememberSaveable { mutableStateOf(0) }
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center
) {
Text(text = "Count: $count", style = MaterialTheme.typography.headlineMedium)
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { count++ }) {
Text("Increment")
}
}
}
@Preview(showBackground = true)
@Composable
fun PreviewRememberSaveableDemo() {
RememberSaveableDemo()
}We use rememberSaveable to create a state variable count that survives configuration changes like screen rotations. This means when the user rotates the device, the count value does not reset to zero.
The UI shows the current count and a button labeled 'Increment'. When the button is tapped, the count increases by 1 and the UI updates automatically.
The Column arranges the text and button vertically with some spacing for a clean layout.