Hello,
I reproduced bug in my game - when I run game, then change language in settings to some other, and then resume game, then I have a crash. What’s the lifecycle of process / activity / Monogame when user changes languange ? As per https://developer.android.com/guide/topics/resources/runtime-changes.html, I think activity is recreated but what happends with Monogame framework ? Which callbacks are called ?
Suppose I have some singletons, which I initialize in Game.Initialize(). This is how it looks:
public static void Initialize()
{
if (INSTANCE != null)
{
throw new Exception("AppSettings instance is already initialized");
}
INSTANCE = new AppSettings();
}
When I change language, activity is recreated and so on Game.Initialize() is called, but I have already this singleton instance created and it leads to exception. Should I dispose somewhere this singleton ? Where and when should I do that ?
Maybe don’t create that singleton if it already exist or if you need to recreate it, check if it exists and then if it does dispose it then make a new.
Bugs like that are the reason I prefer static classes to singletons. You know,
public static class globalstuff {
public static string info1="Yeah, I know it's terrible, but";
public static string info2="it's actually more stable than singletons";
public static int numberSingletonsGaveMeUnexplainableBugs = 23;
public static int numberStaticClasesGaveMeUnexplainableBugs = 0;
}
Thank you for your reply. This is not the solution I’m looking for - recreate singleton when it exists smells like anty pattern.
Generally, the question is very simple - what’s the proper way to dispose global objects in Monogame ? It seems that during that situation (language change), Game.UnloadContent() method is not called. How to dispose global objects now ?
you might be able to stop android recreating the activity on a language change. We already do this for screen rotation and keyboard showing.
Maybe try
ConfigurationChanges = ConfigChanges.Locale | ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
in your Activity attribute.
Unfortunatelly this does not solved problem (Game.Initialize() is called still). Even if it will work, it will be very undesirable behavior - when user will switch language game and then user will resume game - game still will be in old language. Any other ideas ? It seems like a very basic problem - just dispose a singleton …