I happen to have written an app specifically for testing this behavior. I'd never tested it on my recently acquired Android 6 device, so I did just now. The main activity starts the service in onCreate
and stops it in onBackPressed
.
On my LG-K450 (X Power) running 6.0.1, onTaskRemoved
is called with 100% reliability.
If the service is running in the background, onTaskRemoved
is called, but onDestroy
is not, and the process is killed. If onStartCommand
returned START_STICKY
, the service is restarted almost instantly. (It used to take several seconds.) Otherwise, it's restarted when I relaunch the main activity.
If the service is running in the foreground, onTaskRemoved
is still called, but the service is not terminated. This is new. On older versions of Android, foreground services would be killed and restarted.
If you're not seeing calls to onTaskRemoved
, my first suspicion would be a device-specific bug. Let me guess... Samsung?