title: Removing task kills background process when bound
body: <h3>Background</h3>
  <p>I have an app that uses two processes:</p>
  <ul>
  <li>UI</li>
  <li>Background</li>
  </ul>
  <p>The background process hosts two services:</p>
  <ul>
  <li>A long-running foreground service (started with <code>startService</code> and made foreground using <code>startForeground</code>), <code>ImportantService</code>. It uses <code>START_STICKY</code> to ensure it isn't killed if</li>
  <li>A temporary one for inter-process communication, <code>SettingsService</code>.</li>
  </ul>
  <p>The UI binds to the temporary service in the background process for inter-process communication using <code>bindService(..., ..., Context.BIND_AUTO_CREATE)</code>.</p>
  <h3>Problem</h3>
  <p>If I open the Recents activity on my phone and swipe to kill/remove my activity after it has bound to <code>SettingsService</code>, the entire background process is killed, and <code>ImportantService</code> is consequently killed.</p>
  <h3>Symptoms</h3>
  <ul>
  <li><code>ImportantService</code> is restarted after being killed, probably due to <code>START_STICKY</code>.</li>
  <li>This doesn't happen if the services are moved into the UI process.</li>
  <li>This doesn't happen if I start <code>SettingsService</code> via <code>startService()</code> but don't bind to it. The problem seems to be triggered by binding to the service.</li>
  </ul>
  <h3>What I've Tried</h3>
  <ul>
  <li>Starting <code>SettingsService</code> using <code>startService()</code> before binding to it using <code>bindService()</code>.</li>
  <li>Using each of the available flags for <code>bindService()</code>.</li>
  <li>Unbinding from <code>SettingsService</code> in <code>onPause()</code>. This prevents the problem from happening the first time, but it still happens on subsequent tests.</li>
  <li>Making <code>SettingsService</code> bind to itself.</li>
  </ul>
  <h3>Environment</h3>
  <ul>
  <li>Device: Sony Xperia M</li>
  <li>OS: Android 5.1.1</li>
  <li>Customisation: Cyanogenmod 12.1</li>
  <li>Also reproduced the problem in Genymotion emulator running Android 4.4.4</li>
  </ul>
  <h3>Logcat Main Buffer Logs</h3>
  <pre><code>06-17 07:27:22.633      678-897/? I/ActivityManager﹕ Killing 7666:com.example.myapp/u0a151 (adj 9): remove task
  06-17 07:27:22.650     678-1327/? I/WindowState﹕ WIN DEATH: Window{3e26d1c9 u0 com.example.myapp/.SettingsActivity}
  06-17 07:27:22.769     678-1211/? I/ActivityManager﹕ Killing 32110:com.example.myapp:backgroundprocess/u0a151 (adj 0): remove task
  06-17 07:27:22.898     678-1149/? W/ActivityManager﹕ Scheduling restart of crashed service com.example.myapp/.ImportantService in 1000ms
  06-17 07:27:23.979      678-696/? I/ActivityManager﹕ Start proc 7809:com.example.myapp:backgroundprocess/u0a151 for service com.example.myapp/.ImportantService
  </code></pre>
  <h3>Logcat Event Buffer Logs</h3>
  <pre><code>06-17 20:33:08.482 I/am_finish_activity(  678): [0,395523242,1024,com.example.myapp/.SettingsActivity,clear]
  06-17 20:33:08.484 I/am_destroy_activity(  678): [0,395523242,1024,com.example.myapp/.SettingsActivity,finish-imm]
  06-17 20:33:08.488 I/am_kill (  678): [0,5636,com.example.myapp,9,remove task]
  06-17 20:33:08.640 I/dvm_lock_sample(  678): [system_server,1,ActivityManager,142,ActivityManagerService.java,3410,-,8682,28]
  06-17 20:33:08.644 I/am_proc_died(  678): [0,5636,com.example.myapp]
  06-17 20:33:08.645 I/am_kill (  678): [0,3960,com.example.myapp:backgroundprocess,0,remove task]
  06-17 20:33:08.811 I/wm_task_removed(  678): [1024,removeAppToken: last token]
  06-17 20:33:08.812 I/wm_task_removed(  678): [1024,removeTask]
  06-17 20:33:08.816 I/dvm_lock_sample(  678): [system_server,1,Binder_F,136,ActivityManagerService.java,1230,-,1230,27]
  06-17 20:33:08.819 I/am_proc_died(  678): [0,3960,com.example.myapp:backgroundprocess]
  </code></pre>