I had a search view that worked fine but didn't keep the user input on back navigation (from detail view). According to Keep text in BasicTextField on back navigation, all i had to do was to change
remember
to
rememberSaveable
like so:
val textState = rememberSaveable { mutableStateOf(TextFieldValue(""))
But now I am getting this.
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 = {
IconButton(onClick = {
scope.launch {
scaffoldState.drawerState.open()
}
}) {
Image(
//some image gere
)
}
},
backgroundColor = backgroundColor,
actions = {
val textState = rememberSaveable { mutableStateOf(TextFieldValue("")) }
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
SearchView(state = textState, viewModel)
//rest of code
and the search view (edited for brevity):
@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 to rememberSaveable, and no solutions offered there anyway.
Edit: I solved the problem by initializing the textState with the search text saved into the viewmodel. Works fine, but I'm not providing this as an answer to my own question, as it is a hack, but not the real solution. At least for now, if there is a real solution to this. But if this turns out to be a bug in Compose, then I guess it will be an answer.
val textState = remember { mutableStateOf(TextFieldValue(viewModel.filter)) }