在登录界面中,我有一个名为textUser的文本框,并使用remember来保存其状态。但是当我跳转到另一个页面,然后通过点击返回按钮回到该界面时,textUser变量莫名地被重置了。难道mutableState()的目的不就是为了解决这种情况吗?
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仍保持相同的值。