I have been playing with the states all day trying to figure out why, when I press the power button to bri开发者_开发问答ng 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?
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
精彩评论