Mikhail Kiselyov
Mikhail Kiselyov

Reputation: 55

LibGDX Android Gdx.app.exit() doesnt work, how to close app in another way?

I'm trying to close template app for android (5 sec after render() method is started).
Application is closing, but activity is alive in backgroung and I can back to app if I want.
When I'm using this.dispose() without timer, right in the end of render() method it works fine - no activity to resume Application, but when gdx draw anything this issue occures.
I've tried put in timer: Gdx.app.exit() / this.dispose() / System.exit(-1) and nothing helps if render() method already working some time.

Here is the code:

public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
MyGdxGame instance;
boolean isReadyToClose = false;

@Override
public void create () {
    instance = this;
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
}

@Override
public void render () {

    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();

    if (!isReadyToClose) {

        isReadyToClose = true;

        new Timer().scheduleTask(new Timer.Task() {
            @Override
            public void run() {

                //instance.dispose();
                Gdx.app.exit();

            }
        }, 5);

    }

}

@Override
public void dispose () {
    batch.dispose();
    img.dispose();

    System.exit(-1);
}
}

Upvotes: 2

Views: 493

Answers (2)

user2342558
user2342558

Reputation: 6693

In Android you must call finish() or finishAndRemoveTask() in order to close it properly.

Upvotes: 0

MrStahlfelge
MrStahlfelge

Reputation: 1791

You aren't supposed to exit apps on mobile platforms, Gdx.app.exit is intended to be used on desktop. Exit on Android works by the user pressing the back key and your application not catching it.

Upvotes: 0

Related Questions