fancy fancy - 1 year ago 489
Android Question

Callback when App is killed by swiping

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download