Android 上的 Libgdx app.exit() 未关闭应用程序 [英] Libgdx app.exit() on Android not closing application

查看:14
本文介绍了Android 上的 Libgdx app.exit() 未关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 libGDX 开发的 Android 应用程序中,当用户尝试退出游戏时,我使用 Gdx.app.exit().这会关闭游戏,但当用户重新启动应用程序时,所有 Textures 都会被打乱(超出使用应用程序的点).我注意到,如果我从任务管理器强制关闭应用程序,那么它将正确重启.

In my Android app developed with libGDX I use Gdx.app.exit() when the user tries to exit the game. This closes the game, but when the user restarts the app all the Textures are scrambled (beyond the point of using the app). I noticed that if I force close the app from a task-manager, then it will restart properly.

为什么会这样?

推荐答案

你重新发现了 Java 对象的生命周期(与应用程序的生命周期相关)和纹理对象的生命周期(与与 Activity 的可见性相关联的 OpenGL 上下文).

You have rediscovered the mismatch between the lifetime of Java objects (tied to the life of the application process) and the lifetime of texture objects (tied to the life of the OpenGL context which is tied to the visibility of the Activity).

在应用退出"时,仅退出 Activity,Android 正在后台缓存进程.当您重新启动"应用程序时,Android 只会在同一进程中启动一个新的 Activity.在这种情况下,Activity 正在寻找一个有效的 Java Texture 对象,但它在 OpenGL 上下文中指向"的底层字节已经消失(因为当 Activity 不再可见时,OpenGL 上下文无效).

On app "exit", just the Activity is exited, and Android is caching the process in the background. When you "restart" the app Android just starts a new Activity in the same process. In this case the Activity is finding a valid Java Texture object, but the underlying bytes it "points to" in the OpenGL context are gone (since the OpenGL context is invalidated when the Activity is no longer visible).

修复是在活动创建时重新加载纹理.您必须确保所有包含纹理的对象(以及包含包含纹理的对象等)都与 Activity 生命周期相关联.通常,这意味着避免使用静态变量(这是应用程序生命周期的一部分),但如果需要,您可以跳过循环来使全局变量无效并重新初始化.

The fix is to re-load textures on activity creation. You must make sure all your objects that contain textures (and objects that contain objects that contain textures, etc) are tied to the Activity lifecycle. Generally this means avoiding static variables (which are part of the application lifecycle), but you can jump through hoops to invalidate and re-initialize globals if you want.

这篇关于Android 上的 Libgdx app.exit() 未关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆