1

I have an app that binds a service which saves data to the phone's file manager at various times during the application's life cycle (the app is in the background while it's doing this). If something happens to invalidate the data, the data should be discarded once the service completes. (Note that it does do so successfully as long as the service's life cycle is uninterrupted). However, if I swipe away the app while the service is running, the data is not discarded.

The issue is that the activity's onDestroy() method is never called, while onStop() has already occurred because the app is in the background.

Furthermore, the service's onTaskRemoved() is never called (possibly because it's a bound service? Yes, I put android:stopWithTask="false" in the service's declaration in the manifest), nor is onUnbind() or onTrimMemory() when the app is swiped away. The service and app just kind of end.

How can I know that the service/app is being killed in order to discard data before that happens?

Elliptica
  • 3,928
  • 3
  • 37
  • 68
  • remove `android:stopWithTask="false"` from your manifest, call `startService` and see if `Service#onTaskRemoved` was called – pskink Apr 14 '16 at 02:12
  • Can you explain `Service#onTaskRemove`? I have tried without having `android:stopWithTask` in the manifest, and I've overridden `onTaskRemoved` as well as `onUnbind` and `onTrimMemory` and put log statements at the top of them, and none of them get printed when the app is swiped away. – Elliptica Apr 15 '16 at 02:54
  • `Service#onTaskRemoved` is called – pskink Apr 15 '16 at 14:39
  • Clearly though, it's not, at least in this case – Elliptica Apr 15 '16 at 20:30
  • what API version did you try in on? i run on 4.4 and `onTaskRemoved` **IS** called – pskink Apr 16 '16 at 05:28
  • I'm running on 6.0.1. I think it may have to do with the fact that this is a bound service? I've heard that for bound services, onTaskRemoved is not called. – Elliptica Apr 18 '16 at 19:19
  • So should i run my code on 6.0? – pskink Apr 18 '16 at 19:21
  • You could try if you're willing. What kind of service are you using? E.g. is it bound, or is it unbound (called with `StartService()`) – Elliptica Apr 20 '16 at 00:50
  • why "or"? it is both started and bound (i mentioned to call `startService` in my first comment) – pskink Apr 20 '16 at 05:08
  • The service documentation ([link](http://developer.android.com/guide/components/services.html)) distinguishes between Started services (unbound) and Bound services as two different things. This stackoverflow post's answer defines it the same way ([link](http://stackoverflow.com/questions/25240299/can-anybody-explain-what-is-difference-between-unbound-and-bound-service-in-andr)) – Elliptica Apr 21 '16 at 18:06
  • then make it both bound and started – pskink Apr 21 '16 at 18:10
  • But they are two different things... I'm not sure why yours works, but everything I'm looking at says they can't be both bound and started. Can you point me to some documentation that says they can? – Elliptica Apr 21 '16 at 18:22
  • http://developer.android.com/guide/components/bound-services.html ^F `Binding to a Started Service` is says: `As discussed in the Services document, you can create a service that is both started and bound.` also read http://developer.android.com/guide/components/services.html `Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding` – pskink Apr 21 '16 at 18:35
  • do you see how it works now? – pskink Apr 22 '16 at 03:50
  • I think so. Thank you! – Elliptica Apr 25 '16 at 19:37

0 Answers0