0

I'm developing a game in android with AndEngine, and the current biggest problem is when the phone is locked and unlocked again (about a minute or more in between) the game scene resets.

I did a lot of searching, and most suggest saving with functions that are called when the app is closed. I could do this, but it's a lot of saving. I do know, however, that this IS possible, since it is done in AndEngine examples. one peticularly noticable thing with AndEngine examples is when the phone is locked while the program is in the foreground, the screen turns on again. pressing power once more keeps the screen off until unlocked again, after witch the game is perfectly restored.

I scrolled trough the Manifest file and launcher source code, copied and pasted some stuff, but still no sucess. it seems that the solution is in the Manifest file, so here is the one of my program, including all modifications so far:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andenginetest"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:configChanges="orientation|keyboard|keyboardHidden"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

none of it is having any effect on the problem, even though it's suggested by many people. can someone tell me what I might be doing wrong? thanks in advance!

Laurens Weyn
  • 84
  • 1
  • 6
  • Is your game in `portrait` or `landscape` mode? I encountered this problem with an example in `landscape`, but when I changed it to `portrait` everything worked as expected. –  Jul 15 '12 at 12:21
  • it is in landscape mode. I'll try portrait now – Laurens Weyn Jul 15 '12 at 12:26
  • what the!? it's a miracle! somehow changing to portrait mode fixed the problem!!! thanks so much! no idea how that worked. problem right now is my game is designed for landscape. maybe if I go to portrait first and then switch to landscape, or put it on auto-rotate.... – Laurens Weyn Jul 15 '12 at 12:42
  • If you game is designed in `portrait` when you lock the phone, the lock screen changes to `portrait`, when you resume your activity is reseted. I still haven't found a way to stop that from happening. –  Jul 15 '12 at 12:50
  • On many devices screen off has a fuxed orientation, soif you do not indicate in the manifest that you handle orientation (and on recent targets, related) changes, you will go through a destroy/create cycle. But you should save state. – Chris Stratton Jul 15 '12 at 14:21

2 Answers2

0

I'm guessing it's related to the Activity life-cycle. as you can see in the diagram here http://developer.android.com/reference/android/app/Activity.html

there are several methods which are related to pausing, stopping and starting an activity. You should overwite onPause() and onResume() methods, and perform saving there. once onPause() is called, the operating system has the option of killing your activity, without you having the option of doing anything.

that's might be what happening, so if you overwrite these methods, you can tell the application what to do when paused (meaning, the entire screen is covered by another view - either locked, or a phone call, or another application, etc) and when resumed.

Hope that's what you meant. Good luck!

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • thanks, but that isn't really what I meant. the problem is not when minimised, but when the phone is locked and then unlocked again it resets. the example program managed to avoid this problem somehow, but I can't find it. while onPause would be useful, finding and saving every variable and physics object location, velocity etc. is just too complicated for me, especially if there is a workaround. – Laurens Weyn Jul 15 '12 at 12:12
  • after searching some more, i can't find a workaround this. It seems this is just how the Android OS works. see this SO thread http://stackoverflow.com/questions/3407192/problems-understanding-the-life-cycle-when-screen-goes-off-and-on When the screen is locked and then unlocked, it's almost as if it's restarted. also pay attention to the second answer, with the orientation change. maybe it will solve your problem – La bla bla Jul 15 '12 at 12:31
0

I had the exact same problem, I added this to my manifest in the game activity:

         android:screenOrientation="landscape"
         android:configChanges="keyboard|keyboardHidden|orientation">            
    </activity>

As you can see, you dont have the screenOrientation in your manifest, you're probably just setting the orientation when you create the engine, dunno if this will change anything, but I have it like that, and it worked

oh and don't forget to set the onResumeGame and onPauseGame also:

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

    this.enableAccelerometerSensor(this);
}

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

    this.disableAccelerometerSensor();
}
ricvieira
  • 1,053
  • 7
  • 8