Created
January 28, 2026 19:45
-
-
Save sdetilly/a41b9f102d3481a7a89dc9bbe21378a1 to your computer and use it in GitHub Desktop.
Small demo to prove that Compose is smart in its recomposition and ignores Components where the parameter did not change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel : ViewModel() { | |
| val content: StateFlow<MyContent> | |
| field = MutableStateFlow( | |
| MyContent( | |
| param1 = "param1", | |
| param2 = "param2" | |
| ) | |
| ) | |
| init { | |
| viewModelScope.launch { | |
| delay(5.seconds) | |
| content.update { myContent -> myContent.copy(param1 = "foobar") } | |
| } | |
| } | |
| } | |
| data class MyContent(val param1: String, val param2: String) | |
| class MainActivity : ComponentActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge( | |
| statusBarStyle = SystemBarStyle.light( | |
| scrim = Color(0xFF1C1C1E).toArgb(), | |
| darkScrim = Color(0xFF1C1C1E).toArgb() | |
| ), | |
| navigationBarStyle = SystemBarStyle.dark(Color(0xFF1C1C1E).toArgb()) | |
| ) | |
| val viewModel: MyViewModel by viewModels() | |
| setContent { | |
| AndroidDemoTheme { | |
| DemoApp(viewModel) | |
| } | |
| } | |
| } | |
| } | |
| @Composable | |
| private fun DemoApp(viewModel: MyViewModel) { | |
| val myContent by viewModel.content.collectAsStateWithLifecycle() | |
| Column( | |
| modifier = Modifier.fillMaxSize(), | |
| verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically), | |
| horizontalAlignment = Alignment.CenterHorizontally, | |
| ) { | |
| Text(myContent.param1) | |
| Text(myContent.param2) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment