I have three activities in my app and all those activities requires access to service. So if i start the service three times will it be started three times or only once. And if once will the previous data be erased when service is restarted.
Thanx
I have three activities in my app and all those activities requires access to service. So if i start the service three times will it be started three times or only once. And if once will the previous data be erased when service is restarted.
Thanx
If an Android service is already started, Android will not start the service again. For example calling:
...in several separate activities (for binding to IPC listeners or whatnot), will not create new instances of the service. You can see this by looking at your DDMS, you should see something like:
The remote entry is your service, and will only appear once, you can also see this under Android settings, applications, running services on your device.
As for data being erased when the service restarts, this is preserving state issue, any data you want to survive a restart (like killing the app) should be stored, see http://developer.android.com/guide/topics/data/data-storage.html for more detail.
You can easily check if a service is running with the following code
You can also read more about the lifecycle of a service here: http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
if you are starting service with
startService
then for first time it'sonCreate
method will be called and it does not matter how many times you have started the service but its methodonStartCommand(Intent, int, int)
will be called with respect to your startService call. Service stops when you callstopService
irrespective of how many times you have calledstartService
.Don't forget to release the resources, threads when you stop the servive.
you can refer this doc:
http://developer.android.com/reference/android/app/Service.html