8

I'm seeing some interesting activity flow when android turns off the screen and locks the screen... my app goes through the regular flow, onCreate, onStart and onResume. Then, I let my phone sit there. When the screen goes black, onPause is called. That's fine. Then 5 seconds later, the app is killed (onStop, onDestroy). That's fine too. But immediately after onDestroy is called, onCreate, onStart and onResume are called, restarting the app even though the screen is blank. The app has a load time, and it plays a sound when it starts, so it's kind of creepy when the phone you set down 30 seconds ago starts playing sounds. Why does android kill the app then restart it? Target is 2.1-update, and the phone is a Samsung Captivate. Anyone else seeing this, and know a way to stop it?

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
dwrussell2
  • 101
  • 1
  • 4

2 Answers2

4

It may not be what you are seeing, but on my phone, something like this happens because sleep mode is always in one orientation, and if the app was in the other one it gets killed and recreated in the sleep orientation... rather suboptimal if you ask me.

It sounds to me like you may not have things such as your startup sound tied to sufficiently specific causes. What happens if you rotate the phone while it's "on" ? At any rate, you should be able to detect that the screen is not on and not do (or defer) a real startup.

If an implicit orientation change is the culprit, you can change how your application is treated with these - set things to claim your application can change on the fly instead of having to be recreated.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • I think Chris Stratton is correct here. You might also find a bit more information on how to deal with this in this related question: http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – Julian Nov 28 '10 at 17:05
  • The app is locked to landscape mode. Changing the orientation while it is running has no effect. But I can see how android might be trying to restart it into portrait mode during sleep. Time to go hunting for routines that can tell me whether the screen is on or off. – dwrussell2 Nov 28 '10 at 17:35
  • You might want to try watching logcat while the screen goes to sleep, if you have a unix-like development machine you can do something like adb logcat | tee logfile and go back and look later. If it's doing the sleep orientation thing, you'll see this in the logfile – Chris Stratton Nov 28 '10 at 17:45
  • Yup, looking at the logcat. I see a 'config changed' go through, and soon after onStop is called in my app killing it. How lame. If the phone goes to sleep during the app, I get killed... I don't get to go onPause and onResume like everyone else, I have to start from scratch (I tried overriding all the shutdown functions and ignoring the super in this case, but it just crashes). I'll find someway to code around this. – dwrussell2 Nov 28 '10 at 18:13
  • If you set things up so that you say you handle orientation changes in the running app, you shouldn't be killed and recreated any more. – Chris Stratton Nov 28 '10 at 18:28
  • I'll try that. At the moment, I just set setKeepScreenOn(true), and that prevents it from ever killing me (or sleeping, which is lame). I'll try and handle the orientation changes later on. – dwrussell2 Nov 28 '10 at 18:44
3

In the activity's manifest, for each application add:

android:configChanges="orientation|keyboardHidden"

This tells android you'll handle these two cases yourself - although of course you're lying: you won't do shit to handle them, but Android doesn't know that. This works great for apps/games/etc running for example in landscape mode and never change orientation.

kero
  • 10,647
  • 5
  • 41
  • 51
MacD
  • 783
  • 7
  • 23