Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
var count by remember { mutableStateOf(0) }
RecompositionCounter(count)
Button(onClick = {count++}) {
Text(text = "Increment")
}
}
@Composable
fun RecompositionCounter(newCount: Int) {
println("newCount is $newCount")
var oldCount by remember { mutableStateOf(newCount) }
SideEffect {
oldCount = newCount
Log.i(TAG, "oldCount is ${oldCount}")
}
Column {
Text("NewCounter ${newCount}")
println("new count has been changed from $oldCount to $newCount") //
Без линии Println в журнале показаны следующие результаты, как и ожидалось. < /p>
System.out newCount is 0
MainActivity oldCount is 0
System.out newCount is 1
MainActivity oldCount is 1
< /code>
Тем не менее, когда включена строка Println, результаты журнала происходят в два раза больше. < /p>
System.out newCount is 0
System.out newCount has been changed from 0 to 0
MainActivity oldCount is 0
System.out newCount is 1
System.out newCount has been changed from 0 to 1
MainActivity oldCount is 1
System.out newCount is 1
System.out newCount has been changed from 1 to 1
MainActivity oldCount is 1
Должен ли я предположить, что этот результат связан с тем, что линия Println заставила переоборудование еще раз?
Я учусь о Android JetPack Composeefceect. Вот код для тестирования. < /P> [code]Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { var count by remember { mutableStateOf(0) } RecompositionCounter(count) Button(onClick = {count++}) { Text(text = "Increment") } }
@Composable fun RecompositionCounter(newCount: Int) { println("newCount is $newCount") var oldCount by remember { mutableStateOf(newCount) }
SideEffect { oldCount = newCount Log.i(TAG, "oldCount is ${oldCount}") }
Column { Text("NewCounter ${newCount}") println("new count has been changed from $oldCount to $newCount") // Без линии Println в журнале показаны следующие результаты, как и ожидалось. < /p> System.out newCount is 0 MainActivity oldCount is 0
System.out newCount is 1 MainActivity oldCount is 1 < /code> Тем не менее, когда включена строка Println, результаты журнала происходят в два раза больше. < /p> System.out newCount is 0 System.out newCount has been changed from 0 to 0 MainActivity oldCount is 0
System.out newCount is 1 System.out newCount has been changed from 0 to 1 MainActivity oldCount is 1 System.out newCount is 1 System.out newCount has been changed from 1 to 1 MainActivity oldCount is 1 [/code] Должен ли я предположить, что этот результат связан с тем, что линия Println заставила переоборудование еще раз?