11

I have an application that involves navigation. If the user starts a navigation, a kind of "navigationLifecycleManager" is created. This is stored in the application instance so that it survives configuration changes, switching between acitivities etc..

However if the user "quits" the application, I want to kill some background threads, store some minor data into the application storage and so on. So i need some hook(s) that tell me when the app quits.

  1. The navigation should surviv any activity lifecycle (thats why it's in the application instance anyway)
  2. The navigation should survive pressing the home button.
  3. The navigation should not survive ending the app by pressing "back".
  4. The navigation should not survive when it's killed by swiping it out of the "recent apps" list.

Must of this can be achieved by overriding "onPause" and checking "isFinishing". But this doesn't solve the swiping out of the recent apps list. The swiping doesn't seem to call anything. Neither "onPause", "onDestroy" nor "onTerminate" is called in the activity/application.

fancy
  • 2,077
  • 2
  • 23
  • 45
  • can we catch SIGKILL? or can we "get know" when our application is killed from task manager on windows? – Selvin Apr 25 '14 at 09:21
  • I'm guessing your comment is rhetoric and conclude that we do not get this kind of information. Do you know for sure how Android is ending Apps when they are swiped away? Also I'd still like to know how other people deal with this. There must be a lot of people with similar issues. – fancy Apr 25 '14 at 10:54
  • 2
    we can use service's onTaskRemoved() method to detect swipe. – Yogesh Seralia Feb 11 '17 at 12:27
  • @YogeshSeralia your ans is correct. this is only way to know when is killed. I up voted. – Keyur Thumar Mar 22 '17 at 05:31
  • Possible duplicate of [How to handle code when app is killed by swiping in android?](https://stackoverflow.com/questions/19568315/how-to-handle-code-when-app-is-killed-by-swiping-in-android) – Henry Mar 07 '18 at 04:32

1 Answers1

6

You can't handle swipe, because system just removes your process from memory without calling any callback.

I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().

To check back button, use onBackPressed method.

Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60