Search code examples
javaandroidlibgdx

Libgdx Back button exit the app


When I press back button on android, it exits the game although I ask it to go to other screen instead. It goes to the other screen for one or two seconds then it exits.:

if((Gdx.input.isKeyPressed(Input.Keys.BACK)))
    game.setScreen(new GameOverScreen(game,4));
if(controller.isresumePressed()) 
{
    controller.table2visible(false);
    controller.table1visible(true);
    pausestate = false;
}

Solution

  • You also need to tell LibGDX to catch the back key from Android. This only needs to be done once so you can do this in the onCreate() method of your main class.

    Gdx.input.setCatchBackKey(true);