0

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.

Uladzimir
  • 3
  • 2

2 Answers2

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

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

Rafsanjani
  • 4,352
  • 1
  • 14
  • 21
0

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.

Francesc
  • 25,014
  • 10
  • 66
  • 84
  • Taking your advice, I wrote this line: `navController.currentDestination == navController.findDestination(Screen.WeatherForecastScreen.route)`. It is working fine). Hope I didn't disappoint you & understood you correctly. – Uladzimir May 02 '22 at 20:38
  • That's exactly what I described, glad it's working. – Francesc May 02 '22 at 23:05