Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 works fine, when I press BACK in Stage Screen to call Main Screen, it exits the app immediately, without stopping in my Main screen, which is as per my expectations because I am using Gdx.input.isKeyPressed(Keys.BACK) instead of Gdx.input.isKeyJustPressed(Keys.BACK).

But my problem is, when I use Gdx.input.isKeyJustPressed(Keys.BACK) method in my Screens, it does not (return true) catch 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.

share|improve this question

After experimenting long time, I came to the conclusion that it is not possible to catch isKeyJustPressed method without implementing input processor. So I have used isKeyPressed method only, without implementing InputProcessor in my Screens.

I have used a boolean flag to get to know that if back button is kept pressed from last screen, it won't exit in first screen untill unless user push the BACK button again.

public boolean backpressed=false;  //defined in Game class

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)){
            if(!game.backpressed) {
                game.backpressed=true;
            } else if (game.backpressed) {
                game.backpressed=false;
                Gdx.app.exit();
        } 
    }
}

public class StageScreen implements Screen {
    //omitted unnecessary code
    @Override
    public void render(float delta) {
    ...........
    ............
    if (Gdx.input.isKeyPressed(Keys.BACK)) {
        game.backpressed=true;
        game.setScreen(game.MMS);
    }
}
share|improve this answer
    
I have used the same in my android game "Gravity Bits" on play store. You can check that out and mail me for the sample code. – kingAm Sep 14 '16 at 13:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.