Libgdx app.exit()在Android上不关闭应用程序上不、应用程序、app、Libgdx

2023-09-05 23:38:57 作者:纏綿i

在我与libGDX开发的Andr​​oid应用我用 Gdx.app.exit()当用户试图退出游戏。这将关闭游戏,但是当用户重新启动应用程序,所有的纹理扰码(超出使用的应用程序的点)。 我注意到,如果我强迫从任务管理器关闭应用程序,那么它会重新启动正常。

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对象的生命周期(绑在应用程序的生命)和纹理对象的生存期(绑的生活之间的不匹配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).

在应用程序的退出,就在活动退出,而Android是缓存的进程在后台运行。当你重新启动应用程序的Andr​​oid刚开始在同一个进程中新的活动。在这种情况下,活动是找到一个有效的Java Texture对象,但底层的字节为点,在OpenGL上下文都走了(因为当活动不再可见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).

解决方法是重新加载纹理上的活动创造。你必须被连接到活动的生命周期确保所有的对象,它包含纹理(以及含有包含纹理等对象的对象)。一般来说,这意味着避免静态变量(这是应用程序生命周期的一部分),但是你可以赴汤蹈火无效并重新初始化的全局变量,如果你想要的。

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.