[JAVA] Something that can be insisted that Android's repeated hit prevention mechanism and OS malfunction

Main subject

Experiments in which monkeys and rats repeatedly hit buttons and die crazy are famous, but humans also hit buttons repeatedly. That's why the button repeated hit prevention mechanism on Android. The method itself comes out rather, but I can't find the method I thought of, so I'll write it.


public class GuardActivity extends AppCompatActivity {

    //The last time the button was tapped
    private long mLastClickedTime;
    //Repeated hit prevention time.millisecond
    private static long GUARD_TIME = 200;

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        ButtonListener listener = new ButtonListener();

        //ID list of the button to set the listener
        int[] BUTTON_LIST = {R.id.ButtonEnter, R.id.ButtonExit, R.id.ButtonNext, R.id.ButtonPrev};

        for (int buttonId : BUTTON_LIST) {

    //Click listener
    private class ButtonListener implements View.OnClickListener {

        public void onClick(View v) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - mLastClickedTime < GUARD_TIME) {
                //Current time-Last tapped time<Repeated hit prevention time
                //Do nothing in the case of
            //Update last tapped time
            mLastClickedTime = currentTime;

            switch (v.getId()) {
                case R.id.ButtonEnter:
                    //Processing for each button


Please adjust the number of seconds of repeated hit prevention time as appropriate. I haven't confirmed the operation firmly [^ 1].


The reason why I needed this is because the menu created by PreferenceFragmentCompat was hit repeatedly. It seems that you are using PreferenceFragment ** If you hit the menu repeatedly with the Android standard setting application, the same screen will open twice **, so if you are interested and have free time, try hitting it repeatedly. Even if you press different buttons separately in a short time, the screen will open twice [^ 2]. I'm sure I confirmed it with Oreo of Nexus 5X. It may not be possible depending on the model. It's empty and unpleasant to reimplement what's already in the framework, but for a mess like PreferenceFragmentCompat, it's easier and happier to make it like that with the familiar RecyclerView or ListView.

[^ 1]: Because my fingers are tired. [^ 2]: Press → release → press → release at high speed. Practice required.

Recommended Posts

Something that can be insisted that Android's repeated hit prevention mechanism and OS malfunction
Problems that can easily be mistaken for Java and JavaScript