When I lock the screen while my app is running "on top", the system calls almost immediately onCreate (screen is still black). What could be the reason for this destructive behaviour?

- 398,270
- 210
- 566
- 880

- 2,262
- 5
- 28
- 54
-
2`onCreate` is called when the screen orientation is changed and on later version of Android when the screen size changes. Its hard to tell why this is happening to you without any code or XML being posted. To stop `onCreate` being called in some of my applications I added `android:configChanges="keyboardHidden|orientation|screenSize"` to the activities that I did not want to have `onCreate` called when the screen orientation or size changed . – HeatfanJohn May 01 '13 at 14:35
-
+1 Downvoters should add a comment giving critical feedback on how to improve the question when downvoting. – HeatfanJohn May 01 '13 at 14:37
4 Answers
For me I've
android:configChanges="orientation"
but this doesn't help because my activity was full screen and so I added
android:configChanges="keyboardHidden|orientation|screenSize"
in the activity tag
As mentioned in Handling the Configuration Change Yourself
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

- 39,458
- 17
- 135
- 184
-
-
Life saver. configChanges="orientation" was working on my tablet, but not on the phone. Your answer solved this. – Rodia Feb 22 '17 at 16:44
This happens if Activity is in Landscape mode and Lock screen is enabled by user.
There can be two reasons behind this:
If any type of lock screen is enabled and Activity is in Landscape mode: If device has the Lock Screen in Portrait Mode, when the device is locked it automatically switches to portrait mode (Even if your activity was in Landscape Mode). And when the device is unlocked, your Activity becomes visible, but it is again transitioning from Portrait (When locked) to Landscape, so the Activity gets Destroyed and Recreated.
This is how Android Operating System works, it decides when to destroy your view. When you're locking your phone, you app goes to a pause state (onPause) of the activity lifecycle. When an activity is in pause state and if it takes a lot of memory, System has the rights to kill your app (onStop then onDestroy). So when you unlock it, system calls (onCreate) to re-create your view.
Solution:
- You should carefully save and check state using
onSaveInstanceState()
OR - Use
android:configChanges="orientation|screenSize"
for your Activity Tag in Manifest.

- 333
- 1
- 4
- 13
This is how Android OS works, it decides when to destroy your view. When you're locking your phone, you app goes to a pause state (onPause) of the activity lifecycle.
When an activity is in pause state and if it takes a lot of memory, the android system has the rights to kill your app (onStop).
So it must call onCreate to re-create your view when you unlock it.

- 15,697
- 10
- 46
- 64
-
1It actually goes to `onPause` and then to `onStop` when locking the screen. – TronicZomB May 01 '13 at 14:35
-
I though i was meaning this by saying your app goes to a pause state – user2336315 May 01 '13 at 14:36
-
1calling onCreate when the system wants to destroy my activity seems not to be very logical... – user2224350 May 01 '13 at 14:39
-
You sort of are... I can see how both your meaning and the meaning of "it just goes into onPause" can be seen by others. I just wanted to make the clarification is all. – TronicZomB May 01 '13 at 14:39
-
-
Your answer is also true in some cases. But in this scenario, Lock screen has mostly Portrait Mode, so when device is locked and if Activity was in Landscape mode, its orientation is changed. So it destroyed and recreates. – AnujDeo Oct 10 '17 at 11:12
onDestroy() is called when after the screen is back you see the desktop(onDestroy() shutdown app).
After the screen is back and you see the first layout/view of your app then it's called onStop() and onCreate().
After the screen is back and you see the same view before the screen was gone then onPause() and onResume() is called.

- 1