Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
fancy Avatar asked Apr 25 '14 08:04

fancy


People also ask

How do you handle running service when an app is killed by swiping in Android?

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().

Is onDestroy called when app is killed?

When Android decides to kill our app, our activities will call onDestroy method. But before that, one more method will be also called and that is onSaveInstanceState(Bundle). If we check the first method, which is called when opening new activity, onCreate(Bundle), we can see the Bundle parameter.

How do I find app kills on Android?

for User kill( use App switcher ): counter activity number/taskstack etc. (unspecify)for Android Framework kill(Low Memory etc): (unspecify)for Native kill(Process. kill kill signal): use native.

How do I stop an app from killing?

The simplest way to keep background apps in check is using Android's Adaptive Battery feature. Turn it on by going to Settings > Battery > Adaptive preferences and toggle Adaptive Battery on. Update your device!


1 Answers

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.

like image 60
Artem Mostyaev Avatar answered Oct 11 '22 13:10

Artem Mostyaev