Search code examples
javaandroidlibgdx

How to exit a lib gdx application safely?


I have created an app using the lib gdx library, but now when trying to exit the app via the below code Gdx.app.exit();, it do not really exit the app but just hide it (process is cached in the background).

On the first run of the app, the main menu (created using skin) will appear fine enter image description here

But after exiting the app, then start the cached process, you will get issues with the main menu. I try to dispose of the skin in my code but it is still the same. enter image description here

Is there anyway to fully exit the app using the below code as an alternative?

android.os.Process.killProcess(android.os.Process.myPid());

Solution

  • It looks like you are having trouble with Android's life cycle. Rather than resisting it, by manually trying to kill your own process for example, you should learn how LibGDX handles it: For this, take a look at the LibGDX wiki on the app life cycle.

    As to your specific problem, my guess is that you're holding some static reference to your Skin, which means it survives your app "exiting" (after you've read the above article, hopefully you'll understand more about this). But since you disposed of the Skin prior to exiting, then you're using a disposed asset, which won't work. The solution to this problem (if that is indeed the problem) is making sure you are creating a new Skin when the application resumes, rather than using the disposed one. Personally, I never have static references to assets for this reason exactly.