Android onCreate is called after locking the screen
Asked Answered
A

4

13

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?

Andean answered 1/5, 2013 at 14:29 Comment(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 .Val
+1 Downvoters should add a comment giving critical feedback on how to improve the question when downvoting.Val
C
8

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.

Corbie answered 18/10, 2014 at 7:3 Comment(2)
This is also my case.Bistro
Life saver. configChanges="orientation" was working on my tablet, but not on the phone. Your answer solved this.Detect
C
3

This happens if Activity is in Landscape mode and Lock screen is enabled by user.

There can be two reasons behind this:

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

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

  1. You should carefully save and check state using onSaveInstanceState() OR
  2. Use android:configChanges="orientation|screenSize" for your Activity Tag in Manifest.
Chambray answered 10/10, 2017 at 11:9 Comment(0)
D
1

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.

Dotard answered 1/5, 2013 at 14:34 Comment(6)
It actually goes to onPause and then to onStop when locking the screen.Amanda
I though i was meaning this by saying your app goes to a pause stateDotard
calling onCreate when the system wants to destroy my activity seems not to be very logical...Andean
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.Amanda
But not just onStop() is called, onDestroy() is called, too??Andean
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.Chambray
C
0

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.

Coolie answered 18/10, 2013 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.