If an app is forced stop by following the steps Settings->Application->ManageApplications -> --> Force Stop , Does android call the onDestroy of the Activity or the Application ? (If not , Is there any way to know if the application has died due to a force Stop triggered by the User ) .
-
But what about the app Application Protection? are they using a security hole to become root? Because if you force stop the app it starts again, no matter how you kill it, it will start again. – David Nov 22 '12 at 08:08
3 Answers
Force stopping the app will kill the entire process (i.e. with Process.killProcess(int pid)
). All resources associated with the application will be removed and freed by the kernel. So no, there is no way that you can intercept this action.
When you release your application on the market, the developer console will provide you with stats regarding force closes, crashes, etc. (if that is why you are asking).

- 83,063
- 39
- 206
- 250
In short, no, onDestroy()
is not called, and you can't do this. Android doesn't support it.
A more extended answer...
onDestroy()
does not appear to be called in this scenario. I tested this by attempting to have it Toast
me before calling super.onDestroy, but the Toast
message never showed up. (And according to this post, onDestroy()
is really unreliable and won't be called often, if at all, on phones, whereas it may be called on an emulator - so be aware of that). Instead killProcess()
is called, and we cannot intercept that.
In addition, according to the accepted answer in this post, it appears we can't even catch and perform tasks after a user-controlled force stop.
-
As in most cases, there is no guarantee that `onDestroy()` will be called. – Alex Lockwood Jun 22 '12 at 17:42
-
Sorry, I wasn't suggesting you were wrong... just wanted to generalize the answer a little ("as in most cases"). Hope you don't mind. :) – Alex Lockwood Jun 22 '12 at 17:44
-
1Not at all! No need to apologize. Better that you re-state it in other words so it makes it easier to understand, and also signifies the importance of the fact that the user should *not* be relying on onDestroy(). – Mxyk Jun 22 '12 at 17:46
No. This isn't really possible. I haven't looked at the Android code in question, but I would imagine that "force stop" is just calling kill
on the process ID of your app. So the only way you could intercept that is if you could trap signals, which I don't think Android allows.
If you were rooted, you could possibly do this, but not in any standard way.

- 47,727
- 41
- 151
- 191
-
The ability to handle signals would be a pretty big security flaw, in my opinion. :P – Alex Lockwood Jun 22 '12 at 17:48