android jetpackCompose mutablestate不保存状态

p1iqtdky  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(108)

我有一个auth屏幕,在那里我有textUser,我记得要保存状态,但是当我导航到一个屏幕,然后按下返回按钮回到屏幕时,textuser var由于某种原因被重置了。mutuablestate()的全部目的不就是解决这个问题吗?

var textUser by remember { mutableStateOf("") }
 TextField(
                    value = dialCode ?: "+91",
                    onValueChange = { dialCode = it },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
                    modifier = Modifier
//                        .padding(top = 5.dp)
                        .wrapContentWidth()
                        .clickable(onClick = { navController.navigate(NavScreens.Country.route) }),
                    enabled = false,
                    singleLine = true,
                    label = { Text(text = "") },
                    colors = TextFieldDefaults.textFieldColors(
                        containerColor = Color.Transparent,
                        disabledIndicatorColor = Color.Transparent,
                        unfocusedIndicatorColor = Color.Transparent,
                        focusedIndicatorColor = Color.Transparent,
                        focusedLabelColor = Color.Gray,
                        unfocusedLabelColor = Color.Gray
                    ),
                    readOnly = true,
                    textStyle = TextStyle(color = colorResource(id = R.color.login_text_enabled)),
                    interactionSource = remember { MutableInteractionSource() }
                        .also { interactionSource ->
                            LaunchedEffect(interactionSource) {
                                interactionSource.interactions.collect {
                                    if (it is PressInteraction.Release) {
                                        navController.navigate(NavScreens.Country.route)
                                    }
                                }
                            }
                        }
                )
TextField(
                value = textUser,
                onValueChange = { textUser = it },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
                label = { Text(text = stringResource(R.string.email_id_or_mobile_number)) },
                modifier = Modifier.padding(start = hidePadding.dp).align(Alignment.BottomCenter)
                    .fillMaxWidth(),
                colors = TextFieldDefaults.textFieldColors(
                    containerColor = Color.Transparent,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    focusedIndicatorColor = Color.Transparent,
                    focusedLabelColor = Color.Gray,
                    unfocusedLabelColor = Color.Gray,
                    focusedTrailingIconColor = Color.Gray
                ),
                singleLine = true,
                textStyle = TextStyle(color = colorResource(id = R.color.login_text_enabled))
            )

字符串
所以我希望textuser的值是相同的,即使在回到auth屏幕后

iezvtpos

iezvtpos1#

使用rememberSaveable有助于在方向更改或导航时保持状态。
https://developer.android.com/jetpack/compose/state#restore-ui-state

相关问题