我有一个工作正常的搜索视图,但没有将用户输入保留在后退导航上(从详细视图). 根据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)

enter image description here enter image description here

 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.

Stackoveflow

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))}

推荐答案

我不确定为什么你会有这个,也许你必须判断你的组合依赖项版本?

但你实际犯的错误是

MutableState containing TextFieldValue(text='', selection=TextRange(0, 0), composition=null) cannot be saved using the current SaveableStateRegistry. The default implementation only supports types which can be stored inside the Bundle. Please consider implementing a custom Saver for this class and pass it as a stateSaver parameter to rememberSaveable().

您必须创建saver,因为rememberSaveable是在配置更改后仍然存在的东西,并且有某些数据 struct 您必须告诉存储程序它将如何saverestore.

它的行为与Memory类似,但使用保存的实例状态机制(例如,当Android应用程序中的屏幕旋转时),存储的值将在活动或流程重新创建时保留下来.

@Composable
fun <T> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, out Any>,
    key: String? = null,
    init: () -> MutableState<T>
): MutableState<T> = rememberSaveable(

只需创建自定义saver

val textFieldValueSaver = run {
    val textKey = "text"
    mapSaver(
        save = {
            mapOf(textKey to it.text)
        },
        restore = {
            TextFieldValue(it[textKey] as String)
        }
    )
}

只需将其传递给记住可保存的stateSaver参数

val textState = rememberSaveable(
    stateSaver = textFieldValueSaver
) { mutableStateOf(TextFieldValue("")) }

参考文献:Official Docs

Android相关问答推荐

即使安装了Chrome和YouTube,Android对action_view a YouTube URL的意图也是空的

Jetapck Compose:将Compose BOM更新为最新版本&2024.01.00&Quot;CircularProgressIndicator后出现错误

我无法连接到信号机

如何显示具体的商品数量?

如何使用滑行加载媒体的专辑封面?

如何在 Jetpack Compose 中对齐按钮底部中心?

在单元测试下断言协程未完成

列表更改时,RecyclerView 中的列表不会更新

Kotlin 协程、 retrofit 、android

如何用jetpack compose实现垂直李克特量表

未找到 com.android.tools.build:gradle:7.4.0 的匹配变体

在 theme.xml 中使用 android:autoCompleteTextViewStyle

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

导航组件中预期的函数调用map(...)

验证硬编码密码

清洁架构中的服务

Compose `Icons.Outlined.Star` 没有概述

我的 react native 项目的发布签名 apk 没有在设备中打开,而且它创建的尺寸非常小

Android:为什么 ICICI iMobile Pay 应用程序在我的应用程序中显示root/jailbroken设备?

这是 let 函数的正确用法吗?