For completeness sake, I should also explain what I did with settings saving.
I want the settings to save when the game gets minimized, not when it enters the background due to something showing on top of it.
To do this, you have to override the onStop function of your main activity.
Here is mine:
@Override
protected void onStop()
{
// Save settings in the game here, as onStop is called when the application is actually minimized
FileIO.nativeSaveSettings();
super.onStop();
if (m_analytics != null) { m_analytics.onStop(); }
}
The analytics line in there is to forward the stop to my google analytics layer (so I can call reportActivityStop).
The interesting part is the nativeSaveSettings call.
I have a FileIO class in C++ and Java. The jave code for this specific function is simple, as it just calls the C++ function. Like this:
public class FileIO
{
public static native void nativeSaveSettings();
}
There is of course more code in my FileIO class, I just cut out the part you need for this.
The C++ side looks like this:
////////////////////////////////////////////////////////////////////////
// Save Settings (only used by Android)
////////////////////////////////////////////////////////////////////////
#if defined(__COCOS2DX__) && defined(__ANDROID__)
extern "C"
{
void Java_com_gameplayheaven_library_FileIO_nativeSaveSettings( JNIEnv* env, jobject thiz )
{
AppDelegate* pAppDelegate = (AppDelegate*)cocos2d::Application::getInstance();
GameLayer* pGameLayer = pAppDelegate ? pAppDelegate->GetGameLayer() : NULL;
if (pGameLayer) { pGameLayer->SaveSettings(); }
}
}
#endif // __COCOS2DX__ && __ANDROID__
Basically, I catch the call and forward it to my game layer. Which in turn will pass it on to the game specific parts that need to save something (like settings, statistics, scores, etc).
In summary:
All you need to do to prevent the glitch, is NOT call stopAnimation (or any of it’s equivalents), just make sure you pause sound and music.
The other example code is to show where you can catch the real minimize of an app, and you could move the stopAnimation to there. I didn’t, as the game doesn’t receive any updates anymore once it’s minimized anyways…
Hope this helps!