Search code examples
androidlibgdxsplash-screenandroid-lifecycle

launchMode="singleTask" on libgdx isn't working and game restarts on phone lock


I have a project on libgdx and I'm using a splash screen. The problem is, every time I block my phone on main menu and unlock it again, game shows splash screen again and I'd like it not to "restart" the game but stay where it is


EDIT: to avoid publishing all unnecessary code, I'll explain how I made it:

simple class extending game calls setScreen to splash screen, then after 3 seconds splash screen calls main menu, and I'd like the splash screen not to appear again unless the game is DESTROYED and opened again

code:

public MainMenuScreen(final TowerConquest gam) {
    game = gam;
}

@Override
public void show() {
    stage = new Stage(new FillViewport(800, 480));
    Gdx.input.setInputProcessor(stage);
    //((OrthographicCamera)stage.getCamera()).setToOrtho(false, 800, 480);
    atlas = new TextureAtlas("skins/userInterface.pack");
    skin = new Skin(atlas);
    table = new Table(skin);
    table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
    black = new BitmapFont(Gdx.files.internal("font/black.fnt"), false);

    TextButtonStyle textButtonStyle = new TextButtonStyle();
    textButtonStyle.up = skin.getDrawable("normalbutton");
    textButtonStyle.down = skin.getDrawable("pressedbutton");
    textButtonStyle.disabled = skin.getDrawable("dissabledbutton");
    textButtonStyle.pressedOffsetX = 1;
    textButtonStyle.pressedOffsetY = -1;
    textButtonStyle.font = black;
    buttonPlay = new TextButton("PLAY", textButtonStyle);
    buttonPlay.pad(20);
    buttonPlay.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            game.setScreen(new testGame(game));
            //Gdx.input.vibrate(150);
            dispose();
        }
    });

    buttonExit = new TextButton("EXIT", textButtonStyle);
    buttonExit.pad(20);
    buttonExit.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            dispose();
            Gdx.app.exit();
        }
    });

    LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);

    heading = new Label("Tower Conquest", headingStyle);
    table.add(heading);
    table.getCell(heading).padBottom(50);
    table.row();
    table.add(buttonPlay);
    table.getCell(buttonPlay).spaceBottom(15);
    table.row();
    table.add(buttonExit);
    table.debug();
    table.setOrigin(table.getWidth()/2, table.getHeight()/2);
    table.setFillParent(true);
    stage.addActor(table);      
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();
}

@Override
public void resize(int width, int height) {
    my resizes...
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void hide() {
}

@Override
public void dispose() {
    all disposes..
}

Solution

  • I found the answer, add this on manifest file

    android:configChanges="keyboard|keyboardHidden|screenSize|orientation">
    

    it is because when the screen gets locked it changes orientation to portrait so it restarts the app, with that option on, it won't restart the application