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

前端 未结 4 1395
深忆病人
深忆病人 2021-02-20 15:39

What 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 Servic

相关标签:
4条回答
  • 2021-02-20 15:57

    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 讨论(0)
  • 2021-02-20 16:14

    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:

    alt text

    0 讨论(0)
  • 2021-02-20 16:15

    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?

    0 讨论(0)
  • 2021-02-20 16:17

    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..)

    0 讨论(0)
提交回复
热议问题