1

Is it possible in java or in native code? I've tried to catch signals in c++ with
sigaction and in java with Thread.setDefaultUncaughtExceptionHandler(new TryMe());. Nothing works.

mirt
  • 1,453
  • 1
  • 17
  • 35

2 Answers2

1

There is currently no definitive/guaranteed way to detect that an Android app is closing regardless of the closing method. The Android operating system reserves the right to terminate your app at anytime without notice. The recommended solution to requiring this type of notification is simply to make sure you save state by the time the active activity has reached the onStop lifecycle method. If you're trying to restart something that is being killed so that it can continue running in the background, you may need to look into a service. If for some reason you require specifically the case when it is swiped close, I suppose there may be some sort of hack, but it seems unlikely and it would be a hack that would likely stop working in the future as the OS evolves.

Here is a link to a similar question. Handle app closing event from Launcher's menu

Community
  • 1
  • 1
Joey Harwood
  • 961
  • 1
  • 17
  • 27
0

If you have a Service running, you can override onTaskRemoved, this will be called when the user removes your app from the recent task list.

Dokumans
  • 359
  • 1
  • 3
  • 14