Problems understanding the life cycle when screen goes off and on

WarrenFaith picture WarrenFaith · Aug 4, 2010 · Viewed 17k times · Source

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

Answer

Ruben picture Ruben · Nov 14, 2010

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...