开发者

Funky android activity behavior when screen turns off?

开发者 https://www.devze.com 2023-01-27 20:49 出处:网络
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 ther

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 开发者_StackOverflow中文版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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消