Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service onCreate is called multiple times without calling onDestroy

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?

like image 972
SXC Avatar asked Aug 26 '11 22:08

SXC


People also ask

Can onCreate be called multiple times?

OnCreate is only called once.

How many times does onCreate run?

OnCreate will only be called one time for each lifetime of the Activity.

When onCreate function is called in Android?

When onCreate function is called in Android? If one doesn't exist, Android creates one. When Android starts an activity, it calls its onCreate() method. onCreate() is always run whenever an activity gets created.

What is it called when an application is onCreate?

onCreate() - called before the first components of the application starts. onLowMemory() - called when the Android system requests that the application cleans up memory.


1 Answers

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.

like image 173
Dewr Avatar answered Oct 01 '22 22:10

Dewr