I want to know when the app is closed, because I need to erase a Database when the user shutdown the app, just in the moment when the user close the app is the right moment to erase the SQLite Database, how can I detect this?
-
2**Read the docs:** http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Matt Ball Apr 30 '12 at 17:19
-
5Yeah, i know this, but the question is, i need to know when the total application is closed, no when a activity is closed. – JLouis Apr 30 '12 at 17:30
-
What you want is impossible. Your process may be terminated at any time, by the user or the OS, and you are not notified of this. – CommonsWare Apr 30 '12 at 19:06
-
@CommonsWare You seem to be very aware of the Android Stack, is it a possibility to attach an Application.ActivityLifecycleCallbacks to the Application class and listen to onActivityDestroyed(Activity activity) or any termination of the Application class itself? (just wondering) – FeleMed Oct 14 '14 at 18:49
-
@FeleMed: The `Application` is never terminated. The process just ends. The `onTerminate()` method is never called, nor is anything else. – CommonsWare Oct 14 '14 at 19:39
-
You can try firebase on disconnect in the application https://stackoverflow.com/a/46711166/3904109 – DragonFire Apr 18 '20 at 05:25
2 Answers
This is a flawed design idea, which reflects a misunderstanding of the system - when the process overall dies, it's dead, meaning your code is no longer running.
You can do some tracking and have the last onDestory()'d activity do the cleanup as a courtesy, but don't assume that it will always actually happen (the method is not always called). If having a stale copy is a problem, clean it up on the next run.
That said, you can try using the ndk to provide a handler for process termination signals, but still I wouldn't count on it working in all cases. The limited potential to gain any sound functionality from this would probably not justify the effort unless you are already familiar with the concepts involved.
And do not for a minute mistake cleaning up for a security mechanism, as the file is there while your app is running, and would remain if your app terminated in an unexpected way.

- 39,853
- 6
- 84
- 117
-
1"If having a stale copy is a problem, clean it up on the next run." - thanks, good suggestion – Nactus Aug 16 '16 at 17:24
Supposing you don't finish()
your main activity, clearing your database inside the onDestroy()
method of that activity might be the closest of what you want to accomplish. As has been pointed in the comments, refer to http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle.

- 2,232
- 1
- 15
- 24
-
4onDestroy() doesnt seem to work at times, android just skips executing the onDestry() method. – San Nov 25 '15 at 11:26
-
5Using `onPause()` is encouraged, as `onDestroy()` execution is not 100% reliable (it pends on system resources) – Joaquin Iurchuk Jan 22 '16 at 11:49
-
6
-
It does not make any sense because android allows multiple activity stack. Moreover, the user swipes the app out, onDestroy is not called – Jeff Bootsholz Mar 31 '23 at 04:45