最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Android Studio Profiler error with navController with CoroutineScope(). Can anyone give me why? - Stack Overflow

programmeradmin0浏览0评论

I did solve the problem by just simply removing coroutineScope from navigate function, but I'd be grateful if someone can give me why.

My App starts from Route.Init and moves to Route.Home by using NavMulticlickPreventer object. I used this one to ignore multiple clicks on navigation since I run api requests in builder body of navController.

It doesn't have any problem when normally run. But when run with Profiler, app gets infinitely reset on Route.Home with delay in resetAfterDelay.

I am using android studio 2024.2.1.11 and while it is getting reset, it prints this log:

WindowOnBackDispatcher com.myapp.test W sendCancelIfRunning: isInProgress=falsecallback=androidx.activity.OnBackPressedDispatcher$Api34Impl$createOnBackAnimationCallback$1@4009e08

navController = navController,
startDestination = Route.Init
) {
    composable<Route.Init> {}
    composable<Route.Home> {}
} 

NavMultiClickPreventer.navigate(
    navController,
    Route.Home
){
    popUpTo(0) { inclusive = true }
    launchSingleTop = true
}

object NavMultiClickPreventer {
    private val isNavigated = mutableStateOf(false)
    private val scope = CoroutineScope(Dispatchers.Unconfined)

    fun navigate(navController: NavController, targetRoute: Route, builder: NavOptionsBuilder.() -> Unit = {}) {
        if (!isNavigated.value) {
            isNavigated.value = true
            navController.navigate(targetRoute){
                builder()
            }
            resetAfterDelay()
        }
    }

    private fun resetAfterDelay() {
        scope.launch {
            delay(100)
            isNavigated.value = false
        }
    }
}```

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论