You Shoud Add This to Your AndroidManifest.xml

For Android devices you should make sure to include this:

android:configChanges=“orientation|keyboardHidden|screenSize”

This will make sure that your application doesn’t restart on rotation, keyboard slide out or screenSize changes (this is new in Android 4.x)

If you don’t include these, your Cocos2d-x application will restart when the keyboard is pulled out or pushed in.

1 Like

Thanks a lot Cory, why does this happen though, is it a bug on cocos2d-x?

Short Answer: This is how Android works. Cocos2d-x ships with ‘orientation’ covered but hardware proliferation and recent changes to the OS require additional configurations to cover all devices.

In Android, when a configChanges, the Activity is created and destroyed unless the Activity (at run time, or in AndroidManifest) has indicated that it wishes to handle the event. In the case of the normal 2.2 AndroidManifest.xml, any change that isn’t an ‘orientation’ change will result in the Activity (your Cocos2dxActivity) being destroyed and recreated.

Why does Android do this? Because by default, Android destroys and recreates so that your application naturally reloads resources, layouts and images for landscape — or, if you didn’t have the code I posted — new resources, layouts and images for “keyboard out” and resources, layouts and images for “screen slightly smaller due to animated notifications or whatever” which is not what most of us want.

Thanks for the heads up, Cory. That’s how the community should collaborate.