Answer the question
In order to leave comments, you need to log in
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);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question