5

I have been playing with the states all day trying to figure out why, when I press the power button to bring up the lock screen, my app loses focus and calls it's onStop() (as it should) but then it calls the onStart() again before hte screen goes off. This is causing me a problem because some sounds in my app (and presumably other stuff) start playing again while the lockscreen is active.

how can I make sure it is properly backgrounded and stopped when the lockscreen is active?

Hamid
  • 4,410
  • 10
  • 43
  • 72

1 Answers1

8

I faced this exact problem not long ago. In AndroidManifest.xml, make sure you have this:

android:configChanges="keyboardHidden|orientation"

This will prevent your activity from being restarted on runtime 'configuration changes'. See handling the configuration change yourself. That way your app will listen for events that would cause a restart - like orientation and keyboard visibility changes -- and handle them within your Activity.

There is also a very similar question on SO here: Activity restart on rotation Android

Community
  • 1
  • 1
Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
  • 1
    You're a lifesaver thanks! I saw this option this morning when I was looking at my window having problems with losing orientation, and ignored it. Thanks again! – Hamid Dec 15 '10 at 15:49
  • 1
    Quoting from the linked-to documentation, "This technique should be considered a last resort and is not recommended for most applications." – CommonsWare Dec 15 '10 at 16:09
  • The nature of my application means that it should _always_ be in landscape, regardless of any other factors and without exception. As such, I think it's an acceptable solution, and actually helps me solve a secondary problem by locking my application in landscape even between activity switches. – Hamid Dec 17 '10 at 19:23