21

Information: My device is a Nexus One with 2.2 and I have tested two projects, one on 1.5 and one on 2.1.

Problem: I have trouble to understand the life cycle of my application when the screen is turned off and on.

Here is my output

// activity starts
08-04 17:24:17.643: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:24:17.643: ERROR/PlayActivity(6215): onResume executes ...
// screen goes off
08-04 17:24:28.943: ERROR/PlayActivity(6215): onPause executes ...
08-04 17:24:32.113: ERROR/PlayActivity(6215): onStop executes ...
08-04 17:24:32.113: ERROR/PlayActivity(6215): onDestroy executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onResume executes ...
08-04 17:24:32.983: ERROR/PlayActivity(6215): onPause executes ...
// screen goes on
08-04 17:24:47.683: ERROR/PlayActivity(6215): onResume executes ...
// lock removed
08-04 17:24:56.943: ERROR/PlayActivity(6215): onPause executes ...
08-04 17:24:59.663: ERROR/PlayActivity(6215): onStop executes ...
08-04 17:24:59.663: ERROR/PlayActivity(6215): onDestroy executes ...
08-04 17:25:00.943: ERROR/PlayActivity(6215): onStart executes ...
08-04 17:25:00.943: ERROR/PlayActivity(6215): onResume executes ...

I am totally confused. Why restarting the activity when the screen goes off? And why stop and restarting it again when the screen was already on and only the lock was removed?

To make sure I haven't done anything wrong, I created a new project with only this activity. The output is identically...

public class LifeCycleTest extends Activity {

    private final static String DEBUG_TAG = "FirstLifeLog";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(DEBUG_TAG, "onCreate executes ...");
        setContentView(R.layout.main);
    }

    protected void onRestart() {
        super.onRestart();
        Log.e(DEBUG_TAG, "onRestart executes ...");
    }

    protected void onStart() {
        super.onStart();
        Log.e(DEBUG_TAG, "onStart executes ...");
    }

    protected void onResume() {
        super.onResume();
        Log.e(DEBUG_TAG, "onResume executes ...");
    }

    protected void onPause() {
        super.onPause();
        Log.e(DEBUG_TAG, "onPause executes ...");
    }

    protected void onStop() {
        super.onStop();
        Log.e(DEBUG_TAG, "onStop executes ...");
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.e(DEBUG_TAG, "onDestroy executes ...");
    }
}

Does someone have an idea?

Update from today (dont understand why it behaves not like last time, maybe more free resources?)

// activity starts
08-09 12:14:03.122: ERROR/FirstLifeLog(15406): onCreate executes ...
08-09 12:14:03.132: ERROR/FirstLifeLog(15406): onStart executes ...
08-09 12:14:03.132: ERROR/FirstLifeLog(15406): onResume executes ...
// screen off
08-09 12:14:07.412: ERROR/FirstLifeLog(15406): onPause executes ...
// screen on
08-09 12:14:11.722: ERROR/FirstLifeLog(15406): onResume executes ...
// no log for removed screen lock
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • What is strange is that I see onDestroy(), onStart(), onResume() calls but I don't see any onCreate() calls. I am also interested in understanding the behavior. – Catalin Morosan Aug 05 '10 at 06:28
  • Thats because my onCreate missed the log statement... Also I tried it today again... now my game has the same behavior, but the testproject has the expected behavior (see the update) – WarrenFaith Aug 09 '10 at 10:13

4 Answers4

32

I had the same issue with my own game. My game works in landscape only, and when turning off the screen, the android screensaver takes the control (in portrait mode), thus sending an orientationChange that destroys and recreates the activity.

A simple solution is to declare that you will manage yourself screen orientation changes:

<activity ... android:configChanges="orientation" ... >

This is quite easy if your activity is declared to be landscape only (you have to do nothing), but can get harder if your activity can rotate...

Ruben
  • 580
  • 5
  • 11
  • Thanks! Would have taken me probably some time to find that out. Can confirm whats happing with landscape app: recreated once for change to portrait, recreated 2nd time after coming back from screen off. Also the configChanges stuff works. This really should be properly (prominently) documented. – oberstet Feb 16 '12 at 12:55
  • 3
    This probably should be designed as the accepted answer, as although it came a month later it identifies the cause of what was left mysterious in the other. – Chris Stratton Apr 27 '12 at 18:00
  • Does anyone know *how* to detect such a lifecycle ? I mean detecting that onStop is called just because of screen going off and that onStart is gonna be executed soon. – Snicolas Aug 18 '13 at 19:59
5

Ruben's answer is completely correct, but only if your application targets the API level 12 or lower.

But since the API level 13 in addition to the orientation option, you have to declare the screenSize option, because it also gets triggered when a device switches between the portrait and the landscape orientations:

<activity ... android:configChanges="orientation|screenSize" ... >

Otherwise, your activity would still be recreated an additional time when screen goes off on the API 13 or a higher platform.

For reference, see API docs, android:configChanges section notes.

Community
  • 1
  • 1
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
0

Thats the way. If you read the activity life cycle you will see that the steps are pretty much ordered that way. Its not just when your screen goes on and off but also when you chnage the oreintation of the phone. Android recreated the activity following exactly the steps you have mentioned above. Try rotating you screen, you will see then! =)

Shouvik
  • 11,350
  • 16
  • 58
  • 89
  • I think your predacament is very similar to the situation I have mentioned above. It has to have something with the views being changed so the activity is killed and recreated to run with no screen! Its just my guess... – Shouvik Aug 04 '10 at 17:39
-2

See Activity Lifecycle documentation for a good description of the lifecycle, with diagrams.

Most likely your activity is killed with the screen goes off to save resources (battery power). As the documentation states, you can basically be killed anytime that Android wants to free resources. So, you should always design your activities to be able to be stopped and restarted at any time.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 1
    I know the lifecycle documentation. The kill to save resources can't be the reason because it is restarted immediately. And thats exactly my problem. I don't unterstand why it is killed and immediately restarted. I store a lot in the database when the onDestroy() is triggered... The unnecessary app destroys results in long respond times... – WarrenFaith Aug 04 '10 at 15:49
  • I see...I haven't actually tested this theory, but the documentation discusses some configChanges: http://developer.android.com/reference/android/R.attr.html#configChanges that cause the app to be restarted. Its possible that turning on and off the screen fits under uiMode? Regardless, you might want to see if you can make onDestroy more efficient. Maybe save state throughout, so there is less to save at that point? – Cheryl Simon Aug 04 '10 at 16:20