How to prevent the activity from loading twice on pressing the button

后端 未结 19 1939
情深已故
情深已故 2020-12-01 00:15

I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.

I have an activity which loads on click of a butt

19条回答
  •  -上瘾入骨i
    2020-12-01 00:22

    Other very very simple solution if you no want use onActivityResult() is disable the button for 2 seconds (or time you want), is not ideal, but can solve partly the problem is some cases and the code is simple:

       final Button btn = ...
       btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //start activity here...
                btn.setEnabled(false);   //disable button
    
                //post a message to run in UI Thread after a delay in milliseconds
                btn.postDelayed(new Runnable() {
                    public void run() {
                        btn.setEnabled(true);    //enable button again
                    }
                },1000);    //1 second in this case...
            }
        });
    

提交回复
热议问题