13

When I stop a service using the stop button under the Running Services tab, the method onDestroy() is called.

But when I force stop the application, onDestroy() is never called.

Any explainations about this?

or maybe a solution to fire onDestroy() when force-stopped?

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1732887
  • 288
  • 1
  • 2
  • 9

5 Answers5

5

When your force stop an app, exactly that happens - It is Force Stopped. No warning, no callbacks, just stopped. The entire process is killed, and none of the running components (Activities, Services etc) are given any warning.

There is absolutely no guarantee that onDestroy() will be called. Move any application critical code into onPause(), which is called under most circumstances.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 13
    Good answer. However, the question is specifically about Services, which don't pause or have an onPause() method. Compared to other components, Services don't have much of a lifecycle. If there's something important that needs to be done at the end, maybe the service could be written to save as it works and continue where it left off if/when it's restarted. Or, better yet, avoid having users kill your service by being "well behaved" and as short-running as possible. :) – spaaarky21 Jan 03 '13 at 22:12
2

From the documentation:

Once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed... onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage.

To reiterate this point, Force Stop isn't intended to be graceful and exit the app in a caring manner. If you have critical code that must be run each time app finishes you need to run it in onPause().

Sam
  • 86,580
  • 20
  • 181
  • 179
  • What about the case where you use BIND_AUTO_CREATE to start a service in the Activity's onCreate() method and unbind from it in onDestroy()? If the activity is prematurely killed and onDestroy() never called, the service continues running. – Mike Lowery Mar 27 '13 at 02:23
  • I'm not sure what your trying to do, but it's possible the garbage collector might recognize this service is orphaned and obliterate it in a few minutes. – Sam Mar 27 '13 at 16:14
  • 5
    This is the documentation for an Activity - the OP is concerning Service. – RichColours Aug 24 '14 at 08:05
  • 4
    Services do not have onPause() method. Please read the question again. – akos Dec 27 '14 at 17:53
  • 2
    Answer doesn't provide the relevant information asked in the question. Question is in context of service and the documentation link that is given is for Activity. Service doesn;t have pause and stop states. – Prashant May 30 '18 at 04:38
1

When the application gets force stop, Process.killProcess() is called but not onDestroy() function. Go through this link. You will get some idea. Android force Stop callback to application?

Community
  • 1
  • 1
1

I am assuming you have code that you want to execute in onDestroy() referring to your line:

"or maybe a solution to fire onDestroy() when force-stopped?"

The Service method public void onTaskRemoved(Intent rootIntent) is what you are looking for, it will be called when the app is force-stopped.

Wess
  • 779
  • 8
  • 12
  • 1
    don't think that's true. onTaskRemoved() is not guaranteed to be called if you force stop the app. – takecare May 24 '18 at 15:12
  • 4
    onTaskRemoved() in service is called when your app is killed by swiping it from recent apps – chetna Sep 04 '18 at 07:39
  • 1
    Answer from @chetna is correct. This callback only fired when user swipe-to-close your app or it means close the app from task queue/recent task. It wont fire when the app being force-stop. – Cuong Vo May 06 '19 at 06:24
0

I know it's an old question, but I was having the same issue and in my case I was using a binding service, so even after called stopSelf() Android does not call onDestroy() method, in order to force it I need to call unbindService() first

Onivas
  • 161
  • 1
  • 8