3

I am in the final stages of a Cocos2d - Android game and I am having a problem that I can't figure out.

When I lock my tablet (Nexus 7) or when I hit the home button to leave the game, it restarts. It goes right back to the splash screen like the game is opening all over again. For your reference, here is my Main Activity and my Android Manifest:

public class MainActivity extends Activity {

//To get a static reference to context for shared preferences
private static Context context;

protected CCGLSurfaceView _glSurfaceView;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //To get a static reference to context for shared preferences
    MainActivity.context = getApplicationContext();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    _glSurfaceView = new CCGLSurfaceView(this);

    setContentView(_glSurfaceView);
}

//To get a static reference to context for shared preferences
public static Context getAppContext(){
    return MainActivity.context;
}

@Override
public void onStart()
{
    super.onStart();

    CCDirector.sharedDirector().attachInView(_glSurfaceView);

    CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);

    CCDirector.sharedDirector().setDisplayFPS(true);

    CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);

    CCScene scene = Splash.scene();

    CCDirector.sharedDirector().runWithScene(scene);
}

@Override
public void onPause()
{
    super.onPause();

    CCDirector.sharedDirector().pause();
    SoundEngine.sharedEngine().pauseSound();
}

@Override
public void onResume()
{
    super.onResume();

    CCDirector.sharedDirector().resume();
    SoundEngine.sharedEngine().resumeSound();
}

@Override
public void onStop()
{
    super.onStop();

    SoundEngine.sharedEngine().pauseSound();

}

and

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bigfiresoftware.alphadefender"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/adicon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="bigfiresoftware.alphadefender.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" 
        android:hardwareAccelerated="false">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>       
</application>
android:configChanges="keyboard|keyboardHidden|orientation" 
</manifest>

Any/all help is very much appreciated. Thanks.

Atamous
  • 41
  • 3

3 Answers3

3

add below line in your manifest file to your activity.

        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

when you locked the screen the orientation of your screen changes.So that your activity destroys and again it creates.If you ad this line your orientation will not change.

Rama
  • 1,156
  • 1
  • 7
  • 13
1

The answers above didn't fix it but I finally found the answer. This was a tough one.

If anyone else finds this problem:

Remove the targetSdkVersion in AdnroidManifest.xml. Or change to some others, I haven't tried others but removal worked.

android:minSdkVersion="9"
android:targetSdkVersion="17" /> //git rid of this guy
Atamous
  • 41
  • 3
0

add this line in your onstop() method

CCDirector.sharedDirector().end();

and you need to write an onDestroy() method to remove all the instances when you close the game.

Sandeep R
  • 2,284
  • 3
  • 25
  • 51