Search code examples
javaandroideventsbuttonrunnable

Handling multiple queueEvent / Runnable button presses on Android?


When using Java / Android how should I be dealing with multiple fast button presses when using something like queueEvent(new Runnable() {blah blah blah}? When I click the button fast the second event is firing before the first event has completed and messing it up. How do I isolate them and deal with them one at a time? At the moment I have in UI thread...

public void passOnButtonPress(final int buttonId) {
    queueEvent(new Runnable() {
        public void run() {
            mRenderer.handleInteraction(buttonId);
        }});
}

...and in worker thread...

public void handleInteraction(final int buttonId){
    int command =  BlockZOpenGl20.getCommand();
    switch(buttonId)
    { 
    case R.id.rotateYminus: rotateYMinus

I'm using runnable as I'm creating a 3D game and using OpenGL ES 2 so extending...

class MyGLSurfaceView extends GLSurfaceView {

and implementing a renderer...

public class MyGLRenderer implements GLSurfaceView.Renderer

I've read that these should be on their own thread but this is where the game logic is so button presses on UI thread get chucked across...?

Thanks in advance.


Solution

  • I've solved this problem with a work around. I had an onDrawFrame routine that had numerous states controlled by amount of time elapsed...

    @Override
    public void onDrawFrame(GL10 unused) {
    
        if(frame == Blockz.ANIMATION_FRAME_RATE){
            if(animationState == AnimationState.MOVE)
            {
                do something...
                once game logic complete
                animationState == AnimationState.NEUTRAL
    

    I put a guard around the routine that controls user input so it would only run another command once game logic was in understood state.

    public void handleInteraction(final int keyCode){
        if(animationState == AnimationState.NEUTRAL) {