6

Android provides the Service class, which can be useful for background or non-UI operations.

I have a question about Services' lifecycle.

I know that bound services have the lifecycle like following:

  1. Some component starts the Service via bindService() -> onCreate()
  2. onBind()
  3. process
  4. The binding component calls unbindService() -> onUnbind()
  5. onDestroy()

My question is:

Activities usually call unbindService() at onStop(). However, the Activity can be killed without calling onStop() - I mean, when the system memory is low, the only method that must be called is onPause(). onStop() is after onPause(). Before calling onStop(), the Activity can be destroyed.

In this case, the Service didn't get unbindService(), so the Service is still running. Is this right?

Of course, this rarely happens because Services are background by default. (Services are more likely to be killed by system on low memory.) However, a "Foreground" Service has higher priority than the "onPause()ed activity." according to http://developer.android.com/guide/components/processes-and-threads.html . In this case, the binding activity will be killed first.

If this thing happens, the Service does not end? If memory is not low anymore, then the Activity will be created again, but will call bindService() again since it is a new instance. Also, the Activity even may not restart. Isn't this right? What can I do in this case?

Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • I don't have a full answer to your question, however I wanted to point out that a foreground service must have been started via `startForeground()`. Therefore it becomes a started service and is supposed to be able to live on its own. – Joffrey May 22 '13 at 18:44
  • 1
    If the following happens: 1. Activity A binds Service S. 2. Activity A starts a dialog-styled Activity B (A called onPause only. A is on visible process). 3. Activity B binds Service S (S is on foreground process). 4. System is on low memory, so A is killed. (A's onStop not called) 5. B unbinds S. – Naetmul May 22 '13 at 22:36
  • I see what you mean. Anyway, I don't know if Android actually destroys a single component like this. The Android Developer guide on Processes and Threads reads that a whole process might be killed to recover memory, but I didn't see anything regarding killing a single component. (but of course the importance of each process depends on the state of its components) – Joffrey May 23 '13 at 16:16

2 Answers2

6

The Service is killed, but if you have 'return START_STICKY' being returned from the onStartCommand(...) [AND you are starting the service using 'startService(intent)'], the service will start back up again. The Service will start back up even if the Activity is not opened again.

I have run this example - the BoundedAudioService example and tested by killing the activity - the service restarts itself. (By restart I mean, the onStartCommand(...) of the service is called again)

user1406716
  • 9,565
  • 22
  • 96
  • 151
  • Hey, I am using startService and also returning START_STICKY but still i am getting this error in my logcat and service doesn't get restart .. – Wahib Ul Haq Mar 13 '15 at 02:47
0

A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

mcamocci
  • 56
  • 1
  • 4