Obsolete
Status Update
Comments
sa...@gmail.com <sa...@gmail.com> #2
Here's a really detailed StackOverflow answer that seems to point out some things to look at in relation to this behaviour:
http://stackoverflow.com/a/14293528/238753
sa...@google.com <sa...@google.com> #3
Thank you for your feedback. We assure you that we are doing our best to address the issue reported, however our product team has shifted work priority that doesn't include this issue. For now, we will be closing the issue as won't fix obsolete. If this issue currently still exists, we request that you log a new issue along with latest bug report here https://goo.gl/TbMiIO .
sa...@gmail.com <sa...@gmail.com> #4
Tested and confirmed that this bug affects Android 4.4 - 5.1 but was fixed in Android 6.0.
I've attached an updated sample project since the original one needed some tweaks to get it building.
I've attached an updated sample project since the original one needed some tweaks to get it building.
Description
Do these in an activity:
1. Start a foreground service
2. Bind to a *different* service using the Intent.BIND_AUTO_CREATE flag
3. Open the Recents screen and swipe away the task containing this activity
### What happened ###
The entire process is killed immediately, including its foreground service. (Android soon picks this up and restarts the foreground service, presumably because it returns START_STICKY in onStartCommand().)
Here are the relevant logs showing this:
06-24 20:34:13.967 1267-1849/system_process I/ActivityManager﹕ Killing 5498:com.myapp.swipetokillbug/u0a62 (adj 0): remove task
06-24 20:34:14.028 1267-2468/system_process W/ActivityManager﹕ Scheduling restart of crashed service com.myapp.swipetokillbug/.ForegroundService in 1000ms
06-24 20:34:15.126 1267-1305/system_process I/ActivityManager﹕ Start proc 7991:com.myapp.swipetokillbug/u0a62 for service com.myapp.swipetokillbug/.ForegroundService
### Expected behaviour ###
The process should not be killed because it contains a foreground service.
### Affected OS Versions ###
* Only seems to happen in Android 4.4 and higher.
* Still happens in Android M.
* Doesn't happen in Android 4.3.
* Tested in official emulators, Genymotion, a Sony Xperia M, and some Samsung devices.
### Workarounds ###
1. In the foreground service's onTaskRemoved(), launch an activity on task removal
- Fixes the problem about 75% of the time.
- This closes the "Recents" activity on the user, so it alters the normal task-closing experience for the user.
- Seems to depend on timing, so it mightn't be a perfect workaround
2. In the foreground service's onTaskRemoved(), send multiple broadcasts to a registered receiver in the application with the Intent.FLAG_RECEIVER_FOREGROUND flag.
- Seems to nearly always work, depending on timing and the number of broadcasts you send
3. Unbind from the background service before the task is removed