2

I want to send an event to the server whenever my application is terminated, either manually or by OS. Is there any way that I can detect the application termination. Currently I am depending on activity lifecycle's onStop() to detect it. But as onStop() is called even when an activity goes into background, I cannot differentiate between whether app is paused or it is terminated.

Kindly, help. Thanks in advance.

Kanika
  • 714
  • 8
  • 24

4 Answers4

0

You could try onDestroy() which is run when the app is being destroyed by the system. You code would then look something like this:

public void onDestroy() {
super.onDestroy();
//Enter your code here to do stuff
}

However, on destroying your app the variables used by it will be destroyed along with it.

Anirudh
  • 437
  • 2
  • 10
  • 1
    This is ran when the `Activity` it's in is destroyed. It also runs when the `Activity` is just going through a configuration change, for example, being rotated. You can check whether it's changing configuration or actually ending with the `isChangingConfigurations()` method: https://developer.android.com/reference/android/app/Activity.html#isChangingConfigurations() – zsmb13 Mar 14 '17 at 06:07
  • 2
    Hey Anirudh, I tried onDestroy() but when I manually terminated the app, this method was not called. Also in the android documentation: https://developer.android.com/reference/android/app/Activity.html#onDestroy(), it is given " There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away." Hence, I am not sure if this is the right approach if I want to be certain of app termination all the times. Thanks – Kanika Mar 14 '17 at 16:15
  • Ok. That is good to know. So, another approach you can take is having a background service running to monitor the health of the application. – Anirudh Mar 14 '17 at 17:40
0

Well I think the best solution for this is to start the service. Such service that will restart itself when the application is force close or pushed out from recent app trey.

So in restart of such service you can easily notify the server that app is closed. please read out this useful discussion here and here. and let me know if you need any help

Community
  • 1
  • 1
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
0

It can be done by keeping a counter how many Activities are created by you, and how many activities are destroyed. You could use a common BaseActivity to do that or use the ActivityLifecycleCallbacks.

If you just want to know if the App is quiting unexpectedly due to an Exception you could use a global ExceptionHandler see Using Global Exception Handling on android

Third Option: set a flag (i.e. in SharedPreferences) if the app is quit on a regular way.

in your Application class in onCreate you can check (and reset) this flag. So you can do something on the next start of your app.

And last but not least you can just override the onBackpressed method.

The best option depends on your usecase you need to solve

r-hold
  • 941
  • 8
  • 30
  • Even if all the activities are destroyed, the application might still live (e.g. music players with foreground services). – m0skit0 Nov 02 '21 at 14:23
-6

I solved it by using Application.ActivityLifeCycleCallbacks.

https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html

Kanika
  • 714
  • 8
  • 24
  • Please provide reason and correct solution also, instead of one liners which are totally unhelpful. – Kanika Apr 10 '18 at 17:52