Can you track when an android application has been terminated?

你说的曾经没有我的故事 提交于 2019-11-28 01:01:33

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.

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.

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
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!