Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Handling Back Button in Android without implementing InputProcessor

$
0
0

Please advice/clarifyon below issue in libgdx while handling back button in Screens.

I have already gone through these 2 threads,

http://stackoverflow.com/questions/7223723/in-libgdx-how-do-i-get-input-from-the-back-button

Is there a way to capture back button twice in same activity libgdx game

As per suggestion in above two threads, I have done something like this in 2 Screen of my Game,

public class MainMenuScreen implements Screen{
//omitted unnecessary code
@Override
    public void render(float delta) {
    if (Gdx.input.justTouched()) {
            game.setScreen(game.STS);   //Setting 2nd Screen
        }       
        else if (Gdx.input.isKeyPressed(Keys.BACK)){
            System.out.println("mms back");
           //if(Gdx.input.isKeyJustPressed(Keys.BACK))
            Gdx.app.exit();            // Exiting   
        }
}
}

public class StageScreen implements Screen{
    //omitted unnecessary code
@Override
    public void render(float delta) {
            if (Gdx.input.isKeyPressed(Keys.BACK)){
            System.out.println("backed to mms in android");
            //if(Gdx.input.isKeyJustPressed(Keys.BACK))
            game.setScreen(game.MMS);  //going back to Main screen
        }
}

Above code is working fine, when I am pressing BACK in Stage Screen to call Main Screen, it is exiting the app instantly,without stopping in my Main screen, which is as per expected because I am using Gdx.input.isKeyPressed(Keys.BACK) instead of Gdx.input.isKeyJustPressed(Keys.BACK).

But my problem is, when I am using Gdx.input.isKeyJustPressed(Keys.BACK) method in my Screens, it is not returning true for catching BACK key.

I have already set Gdx.input.setCatchBackKey(true) for my Game.

Any idea, why does isKeyJustPressed not responds while isKeyPressed does?

Thanks in Advance.


Viewing all articles
Browse latest Browse all 434

Trending Articles