F
F
FoxInSox2013-12-01 00:12:35
Java
FoxInSox, 2013-12-01 00:12:35

Triggering a click event multiple times

When the button is pressed quickly, the handler can be called multiple times (even if the button is disabled in the handler), leading to double dialog boxes, double network requests, double database entries, and so on. Googling, I realized that the problem has existed for a long time and everyone copes with it in different ways, for example, using a flag in the handler. I solved the problem so far by creating a custom handler that fires no more than 300ms:

public abstract class ClickListener implements View.OnClickListener{

  private long lastCallTime;

  @Override
  public void onClick(View v) {

    if (System.currentTimeMillis() - lastCallTime >= 300) {
      clickHandler(v);
    }

    lastCallTime = System.currentTimeMillis();
  }

  public abstract void clickHandler(View v);

}

Is there a global solution to the problem that does not require the use of its handler?
And what is the reason for this behaviour?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asmforce, 2013-12-17
@asmforce

For some reason, I was unable to reproduce the described error - the handler is executed only once, as it should:
pastebin.com/EVLUsBhx
In ​​general, the entire user interface is processed exclusively in the main UI thread, i.e. the interface is "not responding" while any listener is running. Maybe your problem is somewhere else? Try to do a "clean" experiment.
I only tested on Android 2.3.6 (real device). Maybe this bug appeared in later versions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question