Prevent button to be clicked twice on android

Asked

Viewed 490 times

0

I have a delivery app, where when clicking the button a request is triggered, it turns out that there is a bug that even with a preventive code to enter, sometimes it happens that in thousandths of seconds two or more orders are triggered at the same time, in the case when clicking the button that triggers, I believe that this is occurring due to more than a click by the user on the button (although it would have to be very fast, because soon after appears a dialog).

I was trying to make it not happen by putting the following code right after the button is clicked:

      //prevent two click the same time
        if (SystemClock.elapsedRealtime() - mLastClickTime < 30000) {
            Toast.makeText(getBaseContext(),"Aguarde 30 segundos para solicitar uma nova corrida",Toast.LENGTH_LONG).show();
            return;
        }

        mLastClickTime = SystemClock.elapsedRealtime();
  • Already tried to disable the button after the first click?

  • The button has to be active again after 30 seconds for example...

  • 1

    Use myButton.setEnabled(false); ai after 30 seconds you enable again.

  • I’m wondering if the dialog that exists to confirm that you’re causing this... The process is: clicked on the button, shows a dialog asking if it confirms the request, if it clicks yes again there it fires... @acklay

  • This is a type of problem that Functional Reactive solves very well. You would make a mapSwitch in the click event with a debouce, and the problem is over. This question of double-clicking (or n clicks) with request to the server, even using prefixes or deferreds, are almost impossible to actually resolve with vanilla js. Take a look at lib Rx. Who knows you don’t adopt in the project. Oh, and that without changing the state of the button. Better yet, without maintaining any state! : ) Abc

2 answers

1

You will have to set the method of deactivating the event click button, so you can control when the user can click or not the button, in your case he can click again after the request of the service, the code below can you understand:

    onClick(View v){
        ChamarPedido();
    }


    private void ChamarPedido(){
        myButton.setClickable(false);
        //termine de fazer suas requisições e reeative o botão
        ClienteChamouPedido()
        myButton.setClickable(true);


    }
  • It would be better to use the method setEnabled() instead of setClickable().

  • @ramaral I won’t disagree with you, but could you give me the reason ?

  • The reason has to do with what the user expects from a button. An inactive button, which does not react to the click, has a different aspect of an asset, which reacts to the click. When using setClickable(false), the button will look active but will not react to the click, which may confuse the user.

-2

What I would do is the following, when the customer clicks to place an order ("finalize order") I would create an Intent sending it to the screen or an order status Fragment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.