Skip to content Skip to sidebar Skip to footer

Android: Setonclicklistener To Buttons. Firing Twice When Pressed Too Quick

I have a problem with an app i'm currently making for Android phones. I have an Activity that has two Buttons. The point is if i press both buttons fast enough (that's not a good u

Solution 1:

I actually solved my question. The point was to make my method launchGame(int level) sychronized, so it can't be accessed more than once before it finishes. This way, let's say we have two threads from click listeners, because i pressed the button one and button two very very quick: with a synchronized method, it will be called lauchGame(1), finish the action, and then launchGame(2) with the boolean launched flag setted to true.

This is what i've done:

privatevoidsynchronizedlaunchGame(int level){

    //Just launch the activity if !lanchedif(!launched) {
        launched = true;
        //Start Activity
    }
}

Anyways, many thanks for all of your answers!

Solution 2:

You could use one onclicklistener for both of them and protect all the contents with a mutex.

Solution 3:

You could set the OnClickListener of the "other" Button to null within the one of the clicked Button.

Solution 4:

As mentioned, clear the onclicklistener on anything you don't want called. I'd vote for that. However, you could also try calling 'finish' on this activity in the button click handler. I'm not sure that'll be the flow you want, but I think it'll prevent the other calls (haven't tried it, though).

Solution 5:

try to block the button with your launched state

loadLevel01.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        if (launched) return;            
        launchGame(0);
    }
});

Post a Comment for "Android: Setonclicklistener To Buttons. Firing Twice When Pressed Too Quick"