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

后端 未结 8 735
攒了一身酷
攒了一身酷 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:11

    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

    0 讨论(0)
  • 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:

    <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" >
    
    0 讨论(0)
提交回复
热议问题