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

I did solve the problem by just simply removing coroutineScope from navigate function, but I'd be

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
        }
    }
}```

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742359470a4429102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信