'What is the best way to avoid accidental repeated clicks on Any btn?

My problem was accidentally repeated clicks on LazyVerticalGrid element which is resolved by using:

var enabled by rememberSaveable { mutableStateOf(true) } and val scope = LocalLifecycleOwner.current.lifecycleScope.

LazyVerticalGrid(
        state = lazyVGState,
        cells = GridCells.Fixed(3),
        contentPadding = PaddingValues(bottom = 100.dp)
    ) {
        items(groupMap.keys.toList().sorted()) { item ->
            Column(
                modifier = Modifier.clickable(
                    enabled = enabled,
                ) {
                    enabled = false
                    navController.currentBackStackEntry?.savedStateHandle?.set(
                        CITY_WEATHER_LIST,
                        cityList
                    )
                    navController.navigate(Screen.CityForecastScreen.route)
                    scope.launchWhenStarted {
                        delay(10)
                        enabled = true
                    }
                },

                ) {
                // some elements
            }
        }
    }

If i don't use enabled state, user may open an element for couple times. Looking for community opinion. THX.



Solution 1:[1]

The navigation framework provides an up to date and synchronous view of the navigation state in your app, so the safest way to prevent multiple clicks is by checking if you are still in the navigation destination hosting your LazyList by using

navController.currentDestination

and comparing that against the LazyList screen identifier.

Solution 2:[2]

?fun? NavController.?safeNavigate?(?direction?:? ?NavDirections?) {?    
       currentDestination?.getAction(direction.actionId)?.?run? { navigate(direction) }
?}

And instead of navController.navigate, use navController.safeNavigate with the same arguments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Francesc
Solution 2 Rafsanjani