When the user taps the button multiple times in a short period, then multiple events are being called. As an example, we have a button called 'view cart', and when the user clicks it, 'Cart screen' will be opened. The issue is, if the user clicks 'view cart' button multiple times, 'Cart screen' will be opened multiple times. Below are codes of solution I have found.
// variable to prevent double tapping
private var lastClickTime: Long = 0
... ...
viewDataBinding?.layoutViewCart?.setOnClickListener {
openCart()
}
// Open Cart page
private fun openCart() {
if (SystemClock.elapsedRealtime() - lastClickTime > 500) {
lastClickTime = SystemClock.elapsedRealtime()
... ... ...
}
}
But it's not good to write this code for all components, all clickedListeners. Do we have better solution?