我有一个工作正常的搜索视图,但没有将用户输入保留在后退导航上(从详细视图). 根据Keep text in BasicTextField on back navigation,我所要做的就是改变
remember个
至
rememberSaveable个
如下所示:
VAL TextState=记住可保存{muableStateOf(TextFieldValue(""))
但现在我得到了这个.
Compose Runtime internal error. Unexpected or incorrect use of the Compose internal runtime API (pending composition has not been applied)个
TopAppBar(
elevation = 0.dp,
title = {},
navigationIcon = {
IconBut至n(onClick = {
scope.launch {
scaffoldState.drawerState.open()
}
}) {
Image(
//some image gere
)
}
},
backgroundColor = backgroundColor,
actions = {
VAL TextState=记住可保存{muableStateOf(TextFieldValue("")) }
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
SearchView(state = textState, viewModel)
//rest of code
和搜索视图(为简洁起见编辑):
@Composable
fun SearchView(state: MutableState<TextFieldValue>, viewModel: viewModel) {
val interactionSource = remember { MutableInteractionSource() }
BasicTextField(
value = state.value,
onValueChange = { value -> state.value = value; viewModel.search(state.value.text)} ,
// rest of code
This error was already discussed on stackoverflow, but not in relation 至 rememberSaveable, and no solutions offered there anyway.
Edit: I solved the problem by initializing the textState with the search text saved in至 the viewmodel. Works fine, but I'm not providing this as an answer 至 my own question, as it is a hack, but not the real solution. At least for now, if there is a real solution 至 this. But if this turns out 至 be a bug in Compose, then I guess it will be an answer.
VAL文本状态=记住{ MutableStateOf(TextFieldValue(viewModel.filter))}