While testing my app I realized that if a user pressed the FloatingActionButton
quickly, several times, the onClick call back could be fired multiple times. In my case, this caused the backstack being popped multiple times, as the onPopBackStack
callback bubbles up to the NavHost
where it has access to the navController
and ivokes the popBackStack
method.
fun AddEditTodoScreen(onPopBackStack: () -> Unit, viewModel: AddEditTodoViewModel = viewModel()) {
var isNavigating by remember{
mutableStateOf(false)
}
LaunchedEffect(key1 = true){
viewModel.uiEvent.collect{event : UiEvent ->
when(event){
is UiEvent.PopBackStack -> onPopBackStack
else -> Unit
}
}
}
Scaffold(floatingActionButton = {
FloatingActionButton(onClick = {
if(!isNavigating){
isNavigating = !isNavigating
onPopBackStack()
}
}) {
Icon(imageVector = Icons.Default.Check, contentDescription = "Check")
}
Currently I'm just setting a isNavigating
to true when the FloatingActionButton
is first clicked and if clicked again, it checks if the isNavigating
flag is set to true, if so it does nothing. What would be a better way, if any, to address this?