Avoid button multiple rapid clicks

前端 未结 20 1464
猫巷女王i
猫巷女王i 2020-11-28 02:55

I have a problem with my app that if the user clicks the button multiple times quickly, then multiple events are generated before even my dialog holding the button disappear

20条回答
  •  执念已碎
    2020-11-28 03:20

    My solution, need to call removeall when we exit (destroy) from the fragment and activity:

    import android.os.Handler
    import android.os.Looper
    import java.util.concurrent.TimeUnit
    
        //single click handler
        object ClickHandler {
    
            //used to post messages and runnable objects
            private val mHandler = Handler(Looper.getMainLooper())
    
            //default delay is 250 millis
            @Synchronized
            fun handle(runnable: Runnable, delay: Long = TimeUnit.MILLISECONDS.toMillis(250)) {
                removeAll()//remove all before placing event so that only one event will execute at a time
                mHandler.postDelayed(runnable, delay)
            }
    
            @Synchronized
            fun removeAll() {
                mHandler.removeCallbacksAndMessages(null)
            }
        }
    

提交回复
热议问题