How to prevent double code running by clicking twice fast to a button in Android

谁说我不能喝 提交于 2019-12-01 16:06:17

You can use following code: btn.setEnabled(false);

btn.setOnclickListener(new View.onClickListener(){

      public void onClick(View v) {
            btn.setEnabled(false);

      }
});

You can Disable the view temporarily like this

public static void disableTemporarly(final View view) {

        view.setEnabled(false);
        view.post(new Runnable() {
            @Override
            public void run() {

                view.setEnabled(true);
            }
        });
    }

Edit:

The above solution will work fine. But it will become even better when using the power of Kotlin

1) Create the SafeClikc Listener

class SafeClickListener(
        private var defaultInterval: Int = 1000,
        private val onSafeCLick: (View) -> Unit
) : View.OnClickListener {

    private var lastTimeClicked: Long = 0

    override fun onClick(v: View) {
        if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultInterval) {
            return
        }
        lastTimeClicked = SystemClock.elapsedRealtime()
        onSafeCLick(v)
    }
}

2) Add extension function to make it works with any view, this will create a new SafeClickListener and delegate the work to it

fun View.setSafeOnClickListener(onSafeClick: (View) -> Unit) {

    val safeClickListener = SafeClickListener {
        onSafeClick(it)
    }
    setOnClickListener(safeClickListener)
}

3) Now it is very easy to use it

settingsButton.setSafeOnClickListener {
    showSettingsScreen()
}

Happy Kotlin ;)

Well, that's the expected behaviour...

Launch your new acvitity with SINGLE_TOP flag

Or try setting android:launchMode="singleInstance" for Top20 activity in your AndroidManifest.xml

This solves the issue of multiple Instances of activity, and still play the default animation

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 Button.setOnClickListener(new View.OnClickListener {  
   @Override
   public void onClick(final View v) {
        v.setEnabled(false);
        v.postDelayed(new Runnable() {
         @Override
         public void run() {
            v.setEnabled(true);
         }
        },150); //150 is in milliseconds
    }
 });  
AXSM

This solution fits better to my requirements. Instead using enable/disable view feature. I use the time, first, create a variable to store the last time user pressed the view* then when the user presses the view, capture the time in a variable in order to compare the current moment and the last time user pressed the view, if the differential time between times is too small, which means fast,or extremely fast just ignore it and that's it. But if its a reasonable time to you, set the last time variable with the current time and do whatever else you want to do.

@Override
public void onListItemClick(ListView list, View item, int position, long id) {

    Long now = System.currentTimeMillis();
    if((now - lastTimeSelected) > 500){
        lastTimeSelected = now;
        // your code goes here
    } else {
        Log.i(TAG, "Too fast!");
    }

}

I saw a similar answer on other post, but nobody seems to like this approach.

Add the following code snipet to your Activity definition in Androidmanifest.xml

android:launchMode = "singleTask"

Use frozen variable inside Application class.

That is

public class App extends Application {
public static boolean frozen = false;
}

somewhere while click:

//...
onClick() {
if (frozen) {
return;
}
frozen = true
}
//...

Somewhere to release, for instance, when new Activity has been launched

onCreate (...) {
super.onCreate(...);
frozen = false;
}

in AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:largeHeap="true"
        android:name="com.example.App"
        android:icon="@drawable/icon"
        android:label="@string/app_alias"
        android:theme="@style/AppTheme" >
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!