3

What I want to do is to know when user closes the app from Android Launcher's menu, which gets opened on home button long click:

enter image description here

I want to do some operations when user closes the app.

Actually, I want user to logout each time he leaves the app, but as long as he can close app in this was too, I have to handle this event and then make my operations.

I've tried to google this one, but I couldn't find anything considering this.

I don't want to override onStop() or onDestroy(), as long as the user may come back to the app then, in that case I don't have to make my changes. Don't advice this. I only want to make changes, when user closes app entirely.

So my questions are:

  • Isn't there anything like onApplicationDestroy()?

  • Is it possible to make connection with server and pass some values when user closes app in this way?

azizbekian
  • 60,783
  • 13
  • 169
  • 249

1 Answers1

5

I want to do some operations when user closes the app.

That is not possible. Your process is simply terminated, with no notice.

Isn't there anything like onApplicationDestroy()?

No.

Is is possible to make connection with server and pass some values when user closes app in this way?

No.

Note that there is nothing significantly different between the user swiping your app off the recent-tasks list (what you depict in your question) and various other scenarios, including:

  • Android terminating your background process to free up memory for other applications

  • Users terminating your background process using a third-party task maanger

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have service running in the app, when app get closed, I handle exception got from server through runnable, and stop my service. But as long as the service gets succesfully destroyed, it starts again. It Android terminates my app's process, how does that service survive and start again? – azizbekian Jun 25 '13 at 12:30
  • @andranikAzizbekyan: "It Android terminates my app's process, how does that service survive and start again?" -- you are returning `START_STICKY` or `START_REDELIVER_INTENT` from `onStartCommand()`, perhaps due to a superclass implementation of the method. If you return `START_NOT_STICKY`, then if Android terminates your process due to low memory conditions, Android will not attempt to restart your service later on when memory improves. – CommonsWare Jun 25 '13 at 12:32
  • yea, I returned START_STICKY. Is this the problem? Should I change it to not sticky? – azizbekian Jun 25 '13 at 12:37
  • 1
    @andranikAzizbekyan: `START_STICKY` means that you specifically *want* your service restarted if your process is terminated due to low memory conditions (or things that simulate low-memory conditions, like swiping the app off the recent-tasks list). If that is not the behavior that you want, use `START_NOT_STICKY`. – CommonsWare Jun 25 '13 at 12:42
  • @CommonsWare me too have the same problem. my android service gets close when the user closes my app from the recent apps. is there any solution for this? actually in my service i have given a scheduler that will update the db from server in each 2 hours of time interval. So when the user closes my app from the recent app list, the scheduler process also gets closing! please help! this is the issue i'm facing right now! please help.. is there any other solution? – AndroidManifester Apr 03 '15 at 09:35
  • 1
    @ranjithstar256: If you want to get control every two hours, **please** use `AlarmManager`. [Only have a service running when it is actively delivering value to the user](http://commonsware.com/blog/2014/07/27/role-services.html). Sitting and watching the clock tick is **not** actively delivering value to the user. – CommonsWare Apr 03 '15 at 10:36
  • thanks for your response commonsware.. i implemented alarm manager but that is also getting closed when app is force closed & and some other scenario as well.. i have already gone through your answer regarding this issue in another thread.. will try that.. – AndroidManifester Apr 07 '15 at 11:03
  • "I implemented alarm manager but that is also getting closed when app is force closed" -- if you mean the "Force Stop" button, this is expected. Nothing of your app will run again until the user clicks on your launcher icon on the home screen. "and some other scenario as well" -- feel free to demonstrate any other scenario in which `AlarmManager` does not work. The only other cases that I know of are a reboot (so you have to set up your alarms again) and after an uninstall (in which case your app no longer exists to process the alarms). – CommonsWare Apr 07 '15 at 11:08