Confusion over the Android Activity Lifecycle

半腔热情 提交于 2019-12-22 09:51:01

问题


I have an app which is one activity.

Everything works as I would expect except when I put the phone (a Samsung Galaxy Ace running Gingerbread) to sleep with the button on the side. When I do this, the following are called (in this order):

onPause, onStop, onDestroy, onCreate, onStart, onResume, onPause.

This is without waking the phone up, it is still asleep - screen is off.

Why is the activity killed completely and re-recreated?

Even more bizarre, if the phone is then switched back on the following happens:

onResume, onPause, onStop, onDestroyed, onCreate, onStart, onResume

Even though it's 'ready to go' (sitting in onPause), it gets killed and re-created all over again.

If the app is running and I press Home then the activity is only stopped, not killed, once stopped, I can then put the phone to sleep and then wake it up again and the activity is never killed. It just goes to onRestart etc... which is what I would expect.

This behaviour only occurs when the phone is put to sleep while the activity is running (and in the foreground).

I've read the docs on the Activity Lifecycle but I can't figure out why this behaviour occurs.


回答1:


I have the same behaviour on other Samsung device when the Activity is forced to be shown in landscape (set in AndroidManifest.xml).

This happens because the device lock screen is forced to be shown in portrait and while it is in front of your application, it forces your top level Activity into portrait.

This is nothing to worry about and also nothing you can change.



来源:https://stackoverflow.com/questions/17265192/confusion-over-the-android-activity-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!