Dealing with rapid tapping on Buttons

Posted by Eric Burke on Stack Overflow See other posts from Stack Overflow or by Eric Burke
Published on 2010-05-22T00:27:02Z Indexed on 2010/05/22 0:30 UTC
Read the original article Hit count: 247

Filed under:

I have a Button with an OnClickListener. For illustrative purposes, consider a button that shows a modal dialog:

public class SomeActivity ... {

  protected void onCreate(Bundle state) {
    super.onCreate(state);

    findViewById(R.id.ok_button).setOnClickListener(
      new View.OnClickListener() {
        public void onClick(View v) {
          // This should block input
          new AlertDialog.Builder(SomeActivity.this)
            .setCancelable(true)
            .show();
      }
    });
}

Under normal usage, the alert dialog appears and blocks further input. Users must dismiss the dialog before they can tap the button again.

But sometimes the button's OnClickListener is called twice before the dialog appears. You can duplicate this fairly easily by tapping really fast on the button. I generally have to try several times before it happens, but sooner or later I'll trigger multiple onClick(...) calls before the dialog blocks input.

I see this behavior in Android 2.1 on the Motorola Droid phone. We've received 4 crash reports in the Market, indicating this occasionally happens to people.

Depending on what our OnClickListeners do, this causes all sorts of havoc. How can we guarantee that blocking dialogs actually block input after the first tap?

© Stack Overflow or respective owner

Related posts about android