Please advice/clarifyon below issue in libgdx while handling back button in Screens.
I have already gone through these 2 threads,
https://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.