2

Service: onTaskRemoved or onDestroy very rarely being called on swiping app off on the recent apps list

How am I supposed to stop correctly for example video recording in this case? I just get corrupted (not playable) mp4 file

p.s. android:stopWithTask="false" is set of course (and it's default anyway)

user924
  • 8,146
  • 7
  • 57
  • 139

1 Answers1

0

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?

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • are you sure that you swipe specific app off? not just pressing X button to clear all apps from your recent apps list (with X button `onTaskRemoved` is being called ok for me, but with swipe for specific app isn't) – user924 Aug 30 '17 at 22:42
  • @user924 `onTaskRemoved` is called in either case -- all apps cleared or only my test app. I noticed something new about foreground services; see my edit. – Kevin Krumwiede Aug 31 '17 at 04:36