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

后端 未结 8 813
攒了一身酷
攒了一身酷 2021-01-17 12:42

If i click fast to my button in my Android app, it seems that code behind it runs twice. If i click my menu button twice the activity that has to be launch onclick just star

8条回答
  •  遇见更好的自我
    2021-01-17 13:14

    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:

    
    

提交回复
热议问题