gpt4 book ai didi

android - Jetpack Compose 在方向更改时保存状态

转载 作者:行者123 更新时间:2023-12-04 23:41:17 26 4
gpt4 key购买 nike

我正在使用 Android Jetpack 的 Compose,并且一直在尝试弄清楚如何保存状态以更改方向。
我的思路是让一个类成为 ViewModel。当我使用 Android 的传统 API 时,这通常是有效的。
当信息发生更改时,我使用了 remember {} 和 mutableState {} 来更新 UI。
请验证我的理解是否正确...
remember = 保存变量并允许通过 .value 访问,这允许缓存值。但它的主要用途是在更改时不重新分配变量。
mutableState = 更改某些内容时更新变量。
许多博客文章都说要使用@Model,但是,在尝试该方法时导入会出错。
所以,我添加了一个:ViewModel()
但是,我相信我的内存 {} 正在阻止它按预期工作?
我能指出正确的方向吗?

@Composable
fun DefaultFlashCard() {

val flashCards = remember { mutableStateOf(FlashCards())}


Spacer(modifier = Modifier.height(30.dp))

MaterialTheme {


val typography = MaterialTheme.typography
var question = remember { mutableStateOf(flashCards.value.currentFlashCards.question) }



Column(modifier = Modifier.padding(30.dp).then(Modifier.fillMaxWidth())
.then(Modifier.wrapContentSize(Alignment.Center))
.clip(shape = RoundedCornerShape(16.dp))) {
Box(modifier = Modifier.preferredSize(350.dp)
.border(width = 4.dp,
color = Gray,
shape = RoundedCornerShape(16.dp))
.clickable(
onClick = {
question.value = flashCards.value.currentFlashCards.answer })
.gravity(align = Alignment.CenterHorizontally),
shape = RoundedCornerShape(2.dp),
backgroundColor = DarkGray,
gravity = Alignment.Center) {
Text("${question.value}",
style = typography.h4, textAlign = TextAlign.Center, color = White
)
}
}

Column(modifier = Modifier.padding(16.dp),
horizontalGravity = Alignment.CenterHorizontally) {

Text("Flash Card application",
style = typography.h6,
color = Black)

Text("The following is a demonstration of using " +
"Android Compose to create a Flash Card",
style = typography.body2,
color = Black,
textAlign = TextAlign.Center)

Spacer(modifier = Modifier.height(30.dp))
Button(onClick = {
flashCards.value.incrementQuestion();
question.value = flashCards.value.currentFlashCards.question },
shape = RoundedCornerShape(10.dp),
content = { Text("Next Card") },
backgroundColor = Cyan)
}
}
}


data class Question(val question: String, val answer: String) {
}


class FlashCards: ViewModel() {

var flashCards = mutableStateOf( listOf(
Question("How many Bananas should go in a Smoothie?", "3 Bananas"),
Question("How many Eggs does it take to make an Omellete?", "8 Eggs"),
Question("How do you say Hello in Japenese?", "Konichiwa"),
Question("What is Korea's currency?", "Won")
))

var currentQuestion = 0

val currentFlashCards
get() = flashCards.value[currentQuestion]

fun incrementQuestion() {
if (currentQuestion + 1 >= flashCards.value.size) currentQuestion = 0 else currentQuestion++
}
}

最佳答案

在 Compose 中还有另一种处理配置更改的方法,它是 rememberSaveable .如docs says :

While remember helps you retain state across recompositions, the state is not retained across configuration changes. For this, you must use rememberSaveable. rememberSaveable automatically saves any value that can be saved in a Bundle. For other values, you can pass in a custom saver object.


好像 Mohammad's solution更健壮,但这似乎更简单。

关于android - Jetpack Compose 在方向更改时保存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63733944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com