当我双击同一个项目,或者如果我很快进入每个可组合屏幕,我会收到一个错误,我该如何解决这个问题?我试着改变了一些事情,但我就是解决不了,也找不到任何资源来解决这个问题.

底部导航实现

@Composable
fun BottomNav(
    navController: NavController,
    bottomNavState: MutableState<Boolean>,
) {

    val navItems = listOf(
        Screen.HomeScreen,
        Screen.BookmarkScreen,
        Screen.MyRecipesScreen,
        Screen.FavouriteScreen
    )

    AnimatedVisibility(
        visible = bottomNavState.value,
        enter = slideInVertically(initialOffsetY = { it }),
        exit = slideOutVertically(targetOffsetY = { it }),
        content = {
            BottomNavigation(
                backgroundColor = Color.White,
                elevation = 12.dp
            ) {

                val bottomNavBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = bottomNavBackStackEntry?.destination

                navItems.forEach { item ->

                    BottomNavigationItem(
                        icon = {
                            Icon(painter = painterResource(id = item.icon), contentDescription = "")
                        },
                        label = {
                            Text(text = item.title)
                        },
                        selected = currentDestination?.hierarchy?.any { it.route == item.route } == true,
                        onClick = {

                            navController.navigate(item.route) {


                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true


                            }

                        },
                        selectedContentColor = Color.Black,
                        unselectedContentColor = Color.LightGray,
                        alwaysShowLabel = true,
                    )

                }

            }
        }
    )

}

收到错误

2022-03-05 11:50:10.183 8460-8460/com.im.cookgaloreapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.im.cookgaloreapp, PID: 8460
    java.lang.IllegalStateException: Already attached to lifecycleOwner
        at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:38)
        at androidx.lifecycle.SavedStateHandleAttacher.onRecreated(SavedStateHandleSupport.kt:139)
        at androidx.savedstate.Recreator.reflectiveNew(Recreator.java:90)
        at androidx.savedstate.Recreator.onStateChanged(Recreator.java:62)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:360)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:271)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:313)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:151)
        at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:121)
        at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.kt:188)
        at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.kt:154)
        at androidx.navigation.NavController.updateBackStackLifecycle$navigation_runtime_release(NavController.kt:987)
        at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.kt:892)
        at androidx.navigation.NavController.navigate(NavController.kt:1726)
        at androidx.navigation.NavController.navigate(NavController.kt:1658)
        at androidx.navigation.NavController.navigate(NavController.kt:1980)
        at androidx.navigation.NavController.navigate$default(NavController.kt:1975)
        at androidx.navigation.NavController.navigate(NavController.kt:1961)
        at com.im.cookgaloreapp.ui.components.BottomNavigationKt$BottomNav$3$1$1$2.invoke(BottomNavigation.kt:67)
        at com.im.cookgaloreapp.ui.components.BottomNavigationKt$BottomNav$3$1$1$2.invoke(BottomNavigation.kt:57)
        at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
        at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
        at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:223)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
        at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
        at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:511)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:406)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:419)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:310)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:179)
        at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:98)
        at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:80)
2022-03-05 11:50:10.184 8460-8460/com.im.cookgaloreapp E/AndroidRuntime:     at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1205)
        at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1155)
        at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1095)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:488)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
        at android.app.Activity.dispatchTouchEvent(Activity.java:4125)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:446)
        at android.view.View.dispatchPointerEvent(View.java:14568)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6016)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5819)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5310)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5367)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5333)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5485)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5341)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5542)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5314)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5367)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5333)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5341)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5314)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8080)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8031)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7992)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8203)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:335)
        at android.os.Looper.loop(Looper.java:183)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

推荐答案

这确实是一个导航错误.问题是该选项卡被反复单击.暂时的解决办法是平等返回.


NavigationBarItem(
    icon = {
        Icon(painterResource(id = screen.icon), contentDescription = screen.route)
    },
    selected = currentRoute == screen.route,
    onClick = {

        if (currentRoute == screen.route) {
            return@NavigationBarItem
        }

        navController.navigate(screen.route) {
            popUpTo(navController.graph.findStartDestination().id) {
                saveState = true
            }
            launchSingleTop = true
            restoreState = true
        }
    }
)

Kotlin相关问答推荐

来自SnapshotFlow的单元测试StateFlow仅发出initialValue

最好的方法来创建一个 map 在kotlin从两个列表

使函数同时挂起和取消挂起

合并状态流

如何将消费者放入 Kotlin 的 map 中?

Kotlin 基于参数类型的返回类型推断

Kotlin 获取继承类的默认 hashCode 实现

Gradle:无法创建 ExtensiblePolymorphicDomainObjectContainer

将一个列表元素分组为多个组(indexBy)

在 Kotlin 协程中切换 IO 和 UI 的正确方法是什么?

高效匹配两个 Flux

使用 Discord4j 交叉发布 Discord 消息

Jetpack Compose - 单击 LazyColumn 的项目时应用程序崩溃

关于 Kotlin 函数类型转换的问题

Android 在将 androidx 生物识别更新为 1.0.0-alpha04 后崩溃

Kotlin 静态函数:伴生对象,@JvmStatic @JvmField

如何使用 Kotlin Coroutines 使 setOnClickListener debounce 1 秒?

为什么 Kotlin Pair 中的条目不可变?

Kotlin中具有多个参数的绑定适配器

Kotlin:在何时使用枚举