开发者

What happens to Activities/Services when phone goes to sleep/standby mode?

开发者_如何学JAVAWhat happens to android application and activities and services that belongs to application when the phone/AP goes to sleep mode?Will the framework destroy Activities and Services and OS kills the process?


In case of device sleep, activity's `onPause()' will be called. Read activity lifecycle to understand this.

OS only kills the process when memory/resources are low. Activities are killed first, services are only killed as last resort.

But there is no guarantee they will not be killed. This is why you should rely on system services to call you when you need some work done: use AlarmManager to do call your code periodically or use listeners to notify you of system changes (gps, network, etc..)


When the phone sleeps activities don't get destroyed. I believe all that happens is the activities stay the same but fire the onPause() method.

See this image:

What happens to Activities/Services when phone goes to sleep/standby mode?


What i saw in my application is that only the onPause() method of the main activity (category.LAUNCHER) is called. This happened when the phone went to sleep and before that the main activity of the application had been started.

When any one of the other activities had been started before the phone was going to sleep the first onPause() is called then onStop() and in the end onDestroy() - this is for activities which are category.DEFAULT into the manifest.

I don't know maybe the problem is in my code?


When the phone goes to sleep the onPause() method is called. This method is just a warning to your app. Then depending on the device the CPU may also go to sleep and execution of your code may stop. On most devices this may be anywhere from 10 to 60 seconds after the screen goes black.

It is very unlikely that going to sleep will result in your app being killed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜