为什么在 Android Jetpack Compose 中当全局变量 mydata 改变时,变量 b 不会改变?

huangapple go评论59阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月28日 09:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定