英文:
Why doesn't the variable b change when a global variable mydata changed in Android Jetpack Compose?
问题
0 0 yalpsid ot peek ot wok ti epot t'noD , noitcnufsi si nwo , 1 1 oT
英文:
The var myData=0
is global variable, I find the var a
will change when I click the button, but the var b
keep the value 0, why?
BTW, I have read the article.
In my mind, the var b by remember(myData){ mutableStateOf(myData) }
will be recalculated when myData
changes because I have set a key for remember
.
@Composable
fun ScreenHome(){
var a by remember { mutableStateOf(1) }
var b by remember(myData){ mutableStateOf(myData) }
Button(
onClick = {
a = a + 1
myData = myData + 1
},
modifier = Modifier.fillMaxWidth()
) {
Text(a.toString()+" "+b.toString()) // The b keep to display 0
}
}
PublicPar.Kt
var myData=0
答案1
得分: 1
首先,永远不要在@Composable
范围之外使用变量来设置键值。
在你的代码中,你只重新组合了Button
的lambda,因为myData
的变化不会触发重新组合。
如果你将函数更改为以下内容,你会发现MutableState
实例永远不会改变,因为没有重新组合。
@Preview
@Composable
fun ScreenHome(){
var a by remember { mutableStateOf(1) }
val b = remember(myData) { mutableStateOf(myData) }
Column(
modifier = Modifier.padding(20.dp)
) {
Text(text = "b: $b")
Button(
onClick = {
a = a + 1
myData = myData + 1
},
modifier = Modifier.fillMaxWidth()
) {
Text("a: $a, myData: $myData, b: $b") // b保持显示为0
}
}
}
然后,如果你在Button范围之外读取a
,你将触发重新组合,并看到b实例在每次重新组合时都会更改,新的MyData
分配给MutableState
。
@Preview
@Composable
fun ScreenHome(){
var a by remember { mutableStateOf(1) }
val b = remember(myData) { mutableStateOf(myData) }
Column(
modifier = Modifier.padding(20.dp)
) {
// 读取a触发重新组合
Text(text = "a: $a, b: $b")
Button(
onClick = {
a = a + 1
myData = myData + 1
},
modifier = Modifier.fillMaxWidth()
) {
Text("a: $a, myData: $myData, b: $b") // b保持显示为0
}
}
}
英文:
First of all never use variables outside of @Compsosable
scope to set keys.
In you code you only recompose lambda of Button
because change in myData
does not trigger recomposition.
If you change your function as this you will see that MutableState
instance never changes because there is no recomposition.
@Preview
@Composable
fun ScreenHome(){
var a by remember { mutableStateOf(1) }
val b = remember(myData) { mutableStateOf(myData) }
Column(
modifier = Modifier.padding(20.dp)
) {
Text(text = "b: $b")
Button(
onClick = {
a = a + 1
myData = myData + 1
},
modifier = Modifier.fillMaxWidth()
) {
Text("a: $a, myData: $myData, b: $b") // The b keep to display 0
}
}
}
Then if you read a
outside of Button scope you will trigger recomposition and see that b instance changes on each recomposition with new MyData
assigned to MutableState
.
@Preview
@Composable
fun ScreenHome(){
var a by remember { mutableStateOf(1) }
val b = remember(myData){ mutableStateOf(myData) }
Column(
modifier = Modifier.padding(20.dp)
) {
// Reading a triggers recomposition
Text(text = "a: $a, b: $b")
Button(
onClick = {
a = a + 1
myData = myData + 1
},
modifier = Modifier.fillMaxWidth()
) {
Text("a: $a, myData: $myData, b: $b") // The b keep to display 0
}
}
}
答案2
得分: 1
如果myData
只是一个常规属性,可组合项无法知道何时更改以触发重新组合。这就是为什么您需要使用快照状态持有者的原因。如上建议。
英文:
If myData
is just a regular property, the composable has no way to know when it changes to trigger recomposition. That’s why you need to use a snapshot state holder. As suggested above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论