Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service.onDestroy() is called directly after creation, anyway the Service does its work

Tags:

android

I built a Service and it didn't work as expected, so I debugged it. During debugging, I saw that the service's onDestroy() method is called directly after returning from onCreate(). When I comment out the cleanup that happens in onDestroy(), the service does its work, but this should not be my solution. So my question is, why is onDestroy() called so early and why is the service running anyway? Or how can I prevent onDestroy() from being called at the wrong time?

For your information: I've subclassed IntentService.

Thanks for any help.

Binabik

like image 639
Binabik Avatar asked Sep 27 '11 14:09

Binabik


People also ask

When activity onDestroy is called?

onDestroy( ) is called before the activity is destroyed. The system invokes this callback either because: the activity is finishing (due to the user completely dismissing the activity or due to finish( ) being called on the activity), or.

Is onDestroy guaranteed to be called Android?

Basically, there's never a guarantee that onDestroy() will be called, and in some cases processes such as your app will be killed directly, bypassing the method call anyway.

Is onDestroy guaranteed?

Android Activity onDestroy() is not always called and if called only part of the code is executed. Save this question. Show activity on this post. onDestroy() is not always called.

Does stopSelf call onDestroy?

You should call stopSelf () to stop a service. After you call it, the Android Framework will call onDestroy() method automatically.


1 Answers

If you are subclassing IntentService you should be using onHandleIntent(Intent intent) for the lifecycle of your service. Your service might be moving to onDestroy quickly becuase you do not have code inside of onHandleIntent. Although without your code I cannot say for sure.

Also it might aways move to onDestroy quickly because IntentService is auto threaded for you and might just launch the worker thread which calls onHandleIntent and move to onDestroy.

like image 66
Bobbake4 Avatar answered Oct 07 '22 22:10

Bobbake4