Android service onCreate is called multiple times without calling onDestroy

SXC picture SXC · Aug 27, 2011 · Viewed 8.3k times · Source

In my app, I use a service to communicate with our server. The Service spawns several message queue threads to deal with tasks with different priorities.

This model has been used for about one year without big issues. However, recently, I found some time the onCreate of my service class are called multiple times. onDestroy is never called between two onCreate calls. Therefore, I did not get chance to kill existing threads. Once this behavior happens, the service has duplicate threads inside.

The only thing I have changed is to run the service as foreground service is a user signs in the app. I am wondering whether foreground service cause the problem.

Any ideas?

Answer

Dewr picture Dewr · Oct 16, 2011

I had the same problem when my service used the same process with activities(default). but no more problems when I made my service use another process. I edited my AndroidManifest.xml like below... (added android:process attribute)

<service android:name="kr.co.pkbio.binoo.CacheFileManagerService" android:process=":kr.co.pkbio.binoo.service"/>
<service android:name="kr.co.pkbio.binoo.ActivityStackManagerService" android:process=":kr.co.pkbio.binoo.service"/>

see http://developer.android.com/guide/topics/manifest/service-element.html for information.