12

Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.

Below is the onTerminate() documentation which is only available in the emulated scenario.

public void onTerminate()

Since: API Level 1

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

Does anyone have any other approaches to report back when the user closes the application?

We need to know from a pilot/usability standpoint if we need to incorporate additional functionality into our future production app.

Rob
  • 5,223
  • 5
  • 41
  • 62
Evan Anger
  • 712
  • 1
  • 5
  • 24
  • If you could give some idea of what you want to do when the application is "terminated", we might be able to suggest some ideas on how to accomplish it. – Erich Douglass Sep 29 '11 at 18:08
  • Basically, I'd just like to send a fire-forget request to our internal infrastructure to let us know it was happening. Nothing more really. Knowing the frequency of this happening would impact our product's direction going forward. – Evan Anger Sep 30 '11 at 03:09
  • 1
    While there is no specific api to know that your app was killed by system or user there are many apis that can tell you partially what happens with your app sometimes: If you check isFinishing() within onDestory method you can see if the Activity is closed due to finish() or some other reason (os cleanup for example). Also you can use service to track if a task of your app is removed by the user (removing activity from recents for example) by listening to its onTaskRemoved() method. – MikeL Oct 15 '16 at 22:05

3 Answers3

5

Not sure whether this is going to help you...

In my app, I'm using Activity.onDestroy() to do the cleanup that I need. I have a couple of activities - and have onDestroy() in each of them.

This is the closest I got to doing what I needed - and it actually works quite well.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 3
    Appreciate the response, this approach seems like it would catch unwanted requests. For instance, when the app has been put to the background and the os is looking for space. It would potentially get rid of activity instances in the history stack which would invoke their onDestroy(s). Unless their are other conditions you are checking for in this instance? – Evan Anger Sep 30 '11 at 03:15
  • I don't do any special checking. In onDestroy() I save the state of the activity and then in onCreate() restore that state. While this works fine for my app, I do appreciate that your needs may be different. – Aleks G Sep 30 '11 at 06:05
  • onDestroy will definiatly work. but i want to track application closing event like when we remove application from application stack or device will shutdown because of law battery. is there any solution? – Krupesh Kotecha Sep 25 '15 at 05:14
  • @Aleks, while it is ok to cleanup in onDestory I would not suggest saving states or data in it as onDestory is not guaranteed to be called by the os. From onDestory documentation: "... Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here..." – MikeL Oct 15 '16 at 22:13
  • @MikeL You're absolutely correct. I am not using it for any data/state saving, only cleanup. – Aleks G Oct 15 '16 at 22:26
3

Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.

Users do not close applications on Android.

Does anyone have any other approaches to report back when the user closes the application.

Users do not close applications on Android.

Android, in this respect, behaves much like a Web browser. Users do not close Web applications in Web browsers. They might close the browser. They might close a tab. They might press the home button and navigate to a different site/app. They might choose a bookmark and navigate to a different site/app. They might drag a document into the browser and view it. They might double-click on a desktop icon and view it. And they might click some "logout" link in the currently-viewed Web site/app. Any of these cause the user to leave whatever Web site/app they are in, but none of them would be construed as "closing" the Web site/app in the same manner that clicking the close button on a desktop OS window might be construed as "closing" the desktop app.

As @Aleks G notes, there are various lifecycle methods you can override to find out what the user is doing with respect to an activity. onStop() indicates something else took over foreground input and your current activity is no longer visible. onUserLeaveHint() indicates that the user pressed HOME. And so on. But those are at the activity level, not the application level.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 5
    Killing an application via a task manager app, that would be a scenario where the user closes an application. – Evan Anger Sep 29 '11 at 22:14
  • 2
    @DevanDanger: You are not notified when a user kills an app via a task manager, nor when a user force-stops an app via the Manage Services screen in Settings. – CommonsWare Sep 29 '11 at 22:30
  • 8
    Correct. "a user kills an app via a task manager" equals "user closes the application" Thats all I'm saying there, users close applications. That being said, I was wondering if there was anyway we can track that happening. Sorry for the misdirection. – Evan Anger Sep 30 '11 at 03:05
1

Make a BaseActivity extending Activity in your Application and extends this BaseActivity instead of Activity. In this BaseActivity override the onDestroy() method. Like

 @Override
 protected void onDestroy() {
      super.onDestroy();
     //Write the code that you want to do if the application terminates
 }
Amit Pathak
  • 186
  • 3
  • 3
    this will catch every destruction of activity. instead you may use the launcher activity to detect the application onDestroy event. – Alp Altunel Oct 18 '19 at 13:55