views:

114

answers:

2

Hi,

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
A: 

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.

Mayra
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
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?
Mayra
A: 

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