当 Thread.UncaughtExceptionHandler 调用 navigate() 时,应用停止

App stops when Thread.UncaughtExceptionHandler calls navigate()

提问人:JunJaBoy 提问时间:5/31/2023 更新时间:5/31/2023 访问量:15

问:

我正在尝试使 catch 全局应用程序例外。但是,当捕获异常时,应用程序将停止而不会崩溃。但是,UI 组件;像底部导航一样可以点击,并显示涟漪效应。但永远不要改变屏幕。DmsExceptionHandlerDmsExceptionHandler

internal class DmsExceptionHandler @Inject constructor(
    private val context: Context,
    private val appState: DmsAppState,
) : Thread.UncaughtExceptionHandler {

    override fun uncaughtException(
        thread: Thread,
        exception: Throwable,
    ) {
        when (exception) {
            is CommonException.SignInRequired -> signInRequired()
            else -> {
                signInRequired() // todo replace
            }
        }
    }

    private fun signInRequired() {
        appState.navigateToSignIn()
    }
}

如果我不调用,应用程序不会停止。uncaughtExceptionsignInRequired()

internal fun DmsAppState.navigateToSignIn() {
    println("NAVIGATED TO SIGN IN")
    this.navController.navigate(DmsRoute.Auth.route) {/*
        popUpTo([email protected]) {
            saveState = true
        }*/
    }
}
Android 导航 android-jetpack-compose uncaughtexceptionhandler

评论

0赞 ianhanniballake 5/31/2023
您无法从每个 RuntimeException 中恢复。你试过吗...只是修复您的异常?
0赞 JunJaBoy 5/31/2023
当设备的令牌过期时,应用程序将抛出 TokenExpiredException(from 数据层)。然后,处理程序(来自应用程序层)将捕获此异常,并且屏幕将导航到登录屏幕

答: 暂无答案