How To Prevent Repeating Actions On Double Click On The Button?
Solution 1:
Try to disable the button from being clickable after the first time he was clicked:
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//add this line
button.setClickable(false);
Intentintent=newIntent(getActivity(), CalendarActivity.class);
intent.putExtra("day", 16);
intent.putExtra("month", 12);
intent.putExtra("year", 1996);
startActivityForResult(intent, CALENDAR_ACTIVITY_CODE);
}
});
If this wont help you you can go to your activity in the manifest and add this:
android:launchMode = "singleTop"
Solution 2:
This is actually a surprisingly complex problem. The root of the issue is that UI events (e.g. a button click) cause messages to be "posted" to the end of a message queue. If you are fast enough, it's possible to get many of these messages posted to the message queue before the first one is ever processed.
This means that even disabling the button inside your onClick()
method won't truly solve the problem (since the disabling won't "happen" until the first message is processed, but you might already have three other duplicate messages waiting in the message queue).
The best thing to do is to track some sort of boolean flag and check the flag every time inside onClick()
:
privatebooleanfirstClick=true;
button.setOnClickListener(v -> {
if (firstClick) {
firstClick = false;
//do your stuff
}
});
You have to remember to reset firstClick
back to true whenever you want to re-enable button taps, of course.
Solution 3:
To solve this issue what you can do is use Jake Wharton Rxbinding library which allows you to convert your click events to to stream.
You can add a throttle event which helps to stop the event from specified time
RxView.clicks(button).throttleFirst(2, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread()).
subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
Toast.makeText(getApplicationContext(), "Avoid multiple clicks using throttleFirst", Toast.LENGTH_SHORT).show();
}
});
Post a Comment for "How To Prevent Repeating Actions On Double Click On The Button?"