gpt4 book ai didi

android - Jetpack 组成 : Pending composition has not been applied when rememberSaveable is used

转载 作者:行者123 更新时间:2023-12-05 05:30:22 25 4
gpt4 key购买 nike

我有一个搜索 View 可以正常工作,但没有将用户输入保留在后退导航(从详细 View )。根据Keep text in BasicTextField on back navigation ,我所要做的就是改变

remember

rememberSaveable

像这样:

val textState = rememberSaveable { mutableStateOf(TextFieldValue(""))

但现在我明白了。

Compose 运行时内部错误。意外或不正确地使用 Compose 内部运行时 API(未应用待处理的组合)

enter image description here enter image description here

 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

和搜索 View (为简洁起见进行了编辑):

@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

这个错误已经在 stackoverflow 上讨论过,但与 rememberSaveable 无关,而且那里也没有提供任何解决方案。

Stackoveflow

编辑:我通过使用保存到 View 模型中的搜索文本初始化 textState 解决了这个问题。工作正常,但我没有提供这个作为我自己问题的答案,因为它是一个 hack,但不是真正的解决方案。至少现在,如果有一个真正的解决方案。但如果这最终证明是 Compose 中的错误,那么我想这将是一个答案。

val textState = remember {mutableStateOf(TextFieldValue(viewModel.filter)) }

最佳答案

我不确定你为什么会有这个,也许你必须检查你的 compose 依赖版本?

但是你实际遇到的错误是

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 是在配置更改后仍然存在的东西,并且你必须告诉 saver 它将如何保存某些数据结构恢复它。

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

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

只需创建一个自定义保护程序

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

然后将它传递给 rememberSaveable 的 stateSaver 参数

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

引用:Official Docs

关于android - Jetpack 组成 : Pending composition has not been applied when rememberSaveable is used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74711181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com