onDestroy() in Android service - android

I have 2 questions regarding the onDestroy() method of a service
1.Will the onDestroy() method of a service be called if the service is killed by the system because of lack of memory.
2.Will the onDestroy() method of a service be called if the service is killed because the app that started the service was swiped away by the user in the resent apps screen.

When the service is terminated by system onDestroy will be called, but when your application process is terminated onDestroy may not be called.
onDestroy() will be called IF service is being terminated as a result. (this is not always the case) See this discussion for more info.

First question.
Based on my previous experience and Android dev docs.onDestroy() method of a service is called even if the service is killed by the system.Google Android dev documentation is belowe.
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
Second question.
If you would like to kill service when you close your app so yes. Aneroid services are running on the background and normally are not killed when you destroy you're app.

Related

Android - What happens to a bound service if the binding activity was killed?

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:
Some component starts the Service via bindService() -> onCreate()
onBind()
process
The binding component calls unbindService() -> onUnbind()
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?
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)
A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

How to force android service to be destroyed?

I execute stopSelf as service finishes working (from within service) but it is not destroyed (see using log output in onDestroy()) right after it. It leads that startService uses existing (stopped) service instead of starting new one. How can i force service to be destroyed right after it is stopped?
From Documentation:
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these
situations hold, the service's onDestroy() method is called and
the service is effectively terminated.

If android restarts a Service is onCreate called again?

From my little android knowledge I understand that android OS can kill my service under extreme memory conditions.
I have created a service that returns START_STICKY. The service is meant to run in background.
If android is about to kill my service, will it call onDestroy ?
And when it restarts it would it call onCreate ?
See here, the dev guide. http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
onCreate() is only called when the process starts, which can either be the first time the service is running, or if it was killed on restarted, essentially this is called whenever it starts.
onStartCommand() is called whenever a client calls startService().
When a service is destroyed / completely stopped, Android is supposed to call onDestroy() on that service. I think it's possible for that to not happen (e.g. process is killed not through Android system). In the case of a bound service, this is when there are not more active client binders.
Edit: onCreate() Service starts; onStartCommand()someone uses service; onDestroy()Service is killed / stopped.
If someone calls Context.startService() then the system will retrieve
the service (creating it and calling its onCreate() method if needed)
and then call its onStartCommand(Intent, int, int) method with the
arguments supplied by the client
...
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
http://developer.android.com/reference/android/app/Service.html
EDIT: Quick answer. Yes to both questions

Will we leak the android service connection if the client's process is killed by android?

Condition:
I have a client activity "X" of a remote service (with AIDL) that calls the bindService() in the onCreate() and unbindService() in the onDestroy(). Assume that this activity has been started but not in the foreground (onStop() has happened).
It is said that when android system needs more memory elsewhere it might kill the process of another activity with less priority (possibly "X").
If, says, the android system decides to kill "X"'s process, according to the activity-lifecycle diagram the onDestroy() will not be called if the process is killed when more memory is needed.
http://developer.android.com/guide/topics/fundamentals/activities.html
Question:
Will this cause it to leak the service connection? Is it safer then to bind and unbind service in onStart() and onStop()?
Thanks in advance!
Question: Will this cause it to leak the service connection?
The ServiceConnection object would be in the process of "X" and therefore will go away when that process is terminated.

Is an android service guaranteed to call onDestroy()?

The life cycle diagram of an Activity on an android does not guarantee that onDestroy() would be called, but that the process may be killed and the Activity is removed abruptly. The life cycle diagram of a Service on an android does guarantee that onDestroy() would be called. So I have two questions relating to this difference.
Firstly, if the Service is part of the same process as the Activity, is the Service onDestroy() called, though the Activity onDestroy() is not called? I would think not, as "killing a process" suggest that the operating system is stopping its threads and releasing its resources.
And if that is the case, can a Service-only-process be abruptly killed by the OS?
I'm not sure where you're seeing that a Service is guaranteed to have onDestroy() called. As far as I know, this isn't the case. If you read this page of the docs, it describes the conditions in which a service could be killed. So if you're asking if a process which hosts both an activity and service is being killed, will onDestroy() be called on the service (but not on the activity) then the answer is no; a service's onDestroy() will not necessarily be called. As to whether a service-only process can be abruptly killed by the OS: yes, it can. This is especially true when you have a lot of work to do, and your onStartCommand call only queues up the work to do asynchronously. Then the service will be spending the majority of its time not in the protected onCreate, onStartCommand or onDestroy methods.
There are two things to consider:
Android might decide to shut down a process at some point, when memory
is low and required by other processes that are more immediately
serving the user. Application components running in the process that's
killed are consequently destroyed. A process is started again for
those components when there's again work for them to do. Source
In this case onDestroy() is not called as the Android OS will reclaim resources anyway (this is a basic task of the OS in general).
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from
onDestroy(). Source
So when the Android OS notices that the Service has finished its job and is not needed anymore it will be destroyed. The OS gives the app a chance to release the Service's resources to prevent memory leaks. In this case onDestroy() is called as this is the place where the app can release its resources. Of course in this case the application's process stays untouched (as there may be other Services/Activities running in it).

Categories

Resources