Search code examples
androidoncreateonpause

Android - app's activity stopped, started for unknown reason


Hi I have a long running Android app. Its for a special situation where there is no interactive user. There are no other apps being started because there is no user. But I am seeing my app's main Activity regularly go thru the life-cycle of stop, create, resume, etc. I wrote out a log file as it ran:

2012.03.28 05:31 Main activity created
2012.03.28 05:31 Main activity started
2012.03.28 05:31 Main activity resumed
2012.03.28 05:59 Main activity paused
2012.03.28 05:59 Main activity created
2012.03.28 05:59 Main activity started
2012.03.28 05:59 Main activity resumed
2012.03.28 05:59 Main activity stopped
2012.03.28 06:12 Main activity paused
2012.03.28 06:12 Main activity created
2012.03.28 06:12 Main activity started
2012.03.28 06:12 Main activity resumed
2012.03.28 06:12 Main activity stopped
2012.03.28 06:33 Main activity paused
2012.03.28 06:33 Main activity created
2012.03.28 06:33 Main activity started
2012.03.28 06:33 Main activity resumed
2012.03.28 06:33 Main activity stopped
2012.03.28 08:08 Main activity paused
2012.03.28 08:08 Main activity created
2012.03.28 08:08 Main activity started
2012.03.28 08:08 Main activity resumed
2012.03.28 08:08 Main activity stopped
...

Anyone know why? Thanks.


Solution

  • Android reserves the right to kill any application or component that isn't visible to the user (even those can be killed, but it's a rare occurrence). Most often, this is due to memory pressure, and the system kills applications to free up resources.

    If you have a long-running process that runs in the background and it should run continuously, your best bet is to make it a foreground service. This will put the service at a higher priority than a background service, and should be interrupted less by the system.

    However, unless your service NEEDS to run in the foreground, you should implement your service to gracefully handle being torn down and built up by the system, since it usually kills the service for a good reason. Forcing your service into the foreground might increase the risk of hurting overall device performance, and it does force the presence of a persistent notification in the notification bar, which annoys some users (at least me).