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?
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.
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.
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.
onPause
and then to onStop
when locking the screen. –
Amanda 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.
© 2022 - 2024 — McMap. All rights reserved.
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 stoponCreate
being called in some of my applications I addedandroid:configChanges="keyboardHidden|orientation|screenSize"
to the activities that I did not want to haveonCreate
called when the screen orientation or size changed . – Val