Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems understanding the life cycle when screen goes off and on

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
like image 415
WarrenFaith Avatar asked Aug 04 '10 15:08

WarrenFaith


People also ask

What will happen to the activity life cycle if we change the device orientation?

So when the device orientation changes, first the Activity will disappear for a millisecond when the onPause(), OnStop, and onDestroy() methods are called. After a few milliseconds, the activity will be restarted when the onCreate(), onStart() and onResume() methods are called.

Which activity life cycle method gets called whenever a dialog opens on screen?

First you should understand the Android Lifecycle. As you can see, onPause in called when the activity is Paused, which is when your dialog appears, and onResume after you come back to the activity, when it get focus again.

What is the correct life cycle of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

What is activity explain with lifecycle?

An activity is the single screen in android. It is like window or frame of Java. By the help of activity, you can place all your UI components or widgets in a single screen. The 7 lifecycle method of Activity describes how activity will behave at different states.

What are the activity lifecycle methods that trigger when the user clicks the home button?

There are three key lifecycle methods that deal with when an activity becomes visible or invisible to the user. These methods are onStart() , onStop() , and onRestart() .

Which life cycle methods will call when system kills the process?

In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory. So, android usually will call onDestroy() of your activity before it is killed but it is not guaranteed.


2 Answers

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

like image 159
Ruben Avatar answered Oct 18 '22 14:10

Ruben


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.

like image 42
Alexander Abakumov Avatar answered Oct 18 '22 15:10

Alexander Abakumov