英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论