NavBackStackEntry 无法转换为 androidx.activity.ComponentActivity

问题描述 投票:0回答:1

我有一个 SystemBackButtonHandler 来处理后按,但我在 Logcat 中出现以下错误

java.lang.ClassCastException: androidx.navigation.NavBackStackEntry cannot be cast to androidx.activity.ComponentActivity

我只知道NavBackStackEntry不能转换为ComponentActivity,但我不明白如何解决它。

at com.example.bettehomes.navigation.SystemBackButtonHandlerKt.SystemBackButtonHandler(SystemBackButtonHandler.kt:48)
                                                                                                        at com.example.bettehomes.presentation.authscreens.LoginScreenKt.LoginScreen(LoginScreen.kt:104)
                                                                                                        at com.example.bettehomes.navigation.NavHostKt$AppNavigation$2$3.invoke(NavHost.kt:49)
                                                                                                        at com.example.bettehomes.navigation.NavHostKt$AppNavigation$2$3.invoke(NavHost.kt:48)

                                                                                                        
@Composable
internal fun SystemBackButtonHandler(onBackPressed: () -> Unit) {
    CompositionLocalProvider(
        LocalBackPressedDispatcher provides LocalLifecycleOwner.current as ComponentActivity
    ) {
        ComposableHandler {
            onBackPressed()
        }
    }
}
android-jetpack-compose android-navigation
1个回答
0
投票

我建议使用

BackHandler
效果处理程序来处理后按。

查看此链接,了解有关此效果处理程序的更多信息。

BackHandler(
    enabled = true, // If true, prevent back press and execute onBack() instead.
    onBack = { /* Handle something on back press. */ }
)
© www.soinside.com 2019 - 2024. All rights reserved.