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

huangapple go评论91阅读模式
英文:

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

问题

我正在尝试在 `render()` 方法开始后的 5 秒内关闭 Android 模板应用
应用程序正在关闭但活动仍然在后台保持活跃如果需要的话我还可以返回到应用程序
当我在 `render()` 方法末尾直接使用 `this.dispose()` 而没有定时器时一切正常 - 没有活动可以恢复应用程序但当 gdx 绘制任何内容时就会出现此问题
我尝试过在定时器中使用Gdx.app.exit() / this.dispose() / System.exit(-1)如果 render() 方法已经运行了一段时间这些都没有起作用

以下是代码

```java
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);
    }
}
英文:

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);
}
}

答案1

得分: 0

你不应该在移动平台上退出应用,Gdx.app.exit 应该在桌面上使用。在 Android 上,退出是通过用户按下返回键,而你的应用程序不捕获它来实现的。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月26日 05:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定