When I press the sleep button on my Droid, my activity's oncreate is getting called. Why is that happening? Why would the OS want to call OnCreate when the device is going to sleep? Is there any way to stop it, or at least know it's because the phone was put to sleep?
-
1You sure your app is not restarting due to an orientation change?– Dave MacLeanCommented Nov 10, 2010 at 20:26
-
It's not. It happens with the phone sitting perfectly still on my desk. When I press the sleep button, onDestroy is called, then somehow a new activity is getting started.– iteratorCommented Nov 10, 2010 at 20:41
-
I should point out that it's passing my a valid savedInstanceState when relaunching.– iteratorCommented Nov 10, 2010 at 20:42
-
4I was wrong. This in fact is an orientation change. By adding android:configChanges="orientation" to my manifest, I don't get an activity restart. Thanks!– iteratorCommented Nov 10, 2010 at 23:47
-
Yeah, on my phone for some reason "sleep" is always in the same orientation regardless of the physical position of the device, so the orientation change process (which can mean kill and relaunch) often happens as it prepares to sleep.– Chris StrattonCommented Nov 11, 2010 at 6:25
2 Answers
This happens particularly for activities that are locked in rotation, e.g. have this in the manifest within the activity tags:
android:screenOrientation="portrait"
When you turn the screen off, it reads the accelerometer to determine the true orientation and changes to that before turning off.
So yes, the simple solution is adding configChanges as well, making it look something like this:
<activity
android:name=".MyActivity"
android:screenOrientation="landscape"
android:configChanges="orientation"
/>
You don't need to indicate the screenOrientation, but if you don't whatever orientation it is at when it starts is where it will stay unless you write code to handle the configuration changes.
As a side note, when I started testing my apps on ICS I had to handle a few quirks with a create/destroy/create cycle at the start of some activities. A few extra checks and balances are necessary to make the code universal.
-
Do you have any more information about how this works on ICS? I was experiencing this problem on devices running 2.3.3 and 4.0.4. Your solution works on my 2.3.3 device, but the onCreate() is still getting called after sleeping on my 4.0.4. Commented Jun 4, 2013 at 12:51
-
The problem might be that the device has a hard-coded orientation for the lock screen. To test for this, make your locked orientation the opposite (likely portrait). That might make the extra onCreate go away, and give you a starting point to find how to swallow the irrelevant orientation change. Commented Jun 9, 2013 at 17:37
-
8Thank you. You were right. I found a solution by adding screenSize to the configChanges like this
android:configChanges="orientation|screenSize"
. From the docs: "Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations." Commented Jun 11, 2013 at 9:08
I had the same Prob my app (game) reloaded whenever it come from sleep
here what i did to solve it
i add
<activity
android:configChanges="orientation|screenSize"
/>
hope this will help someone !