Reputation: 5212
I wrote a game using libgdx on Android. When my app crashes I still can hear the music play. I think that I forgot to dispose something. I use assetsmanager to load my resources. I call assetsmanger dispose function in the game class in dispose function of it. I think that it should stop the music and dispose of it.
what I'm doing wrong?
Upvotes: 1
Views: 240
Reputation: 13705
Originally the recommendation would be to release resources using the life cycle methods like "onPause", "onStop", "onDestroy", however, since in some specific cases like this one where the app is crashing and for any reason you just don't get the chance to release resources, there's a last resource that you can use:
public class MyApplication extends Application
{
public void onCreate ()
{
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
//Do here whatever you want to do when the app crashes
}
});
}
}
The "Thread.UncaughtExceptionHandler" gives you the chance to get a callback when an exception was propagated without being handled at any level of the stack, hence, it gives you the chance to do things like "logging the crash to a server" or in your case you might be able to use it to release some extra resources.
Hope it Helps!
Regards!
Upvotes: 1