title: How to simulate Android killing my process
body: <p>Android will kill a process if it is in the background and the OS decides it needs the resources (RAM, CPU, etc.). I need to be able to simulate this behaviour during testing so that I can ensure that my application is behaving correctly. I want to be able to do this in an automated way so that I can test if the application behaves correctly whenever this happens, which means that I'll have to test this in every activity, etc.</p>
  
  <p>I know how to kill my process. That isn't the problem. The problem is that when I kill my process (using DDMS, <code>adb shell kill</code>, <code>Process.killProcess()</code>, etc.) Android does not restart it the same way that it would if the Android OS had killed it itself.</p>
  
  <p>If the Android OS kills the process (due to resource requirements), when the user returns to the application Android will recreate the process and then recreate the <strong>top activity on the activity stack</strong> (calling <code>onCreate()</code>).</p>
  
  <p>On the other hand, if <strong>I</strong> kill the process, <strong>Android assumes that the activity on the top of the activity stack was badly behaved</strong>, so it automatically recreates the process and then <strong>removes the top activity from the activity stack and recreates the activity that was underneath the top activity</strong> (calling onCreate()`). This is not the behaviour I want. I want the same behaviour as when Android kills the process.</p>
  
  <p>Just to explain pictorially, if my activity stack looks like this:</p>
  
  <pre><code>    ActivityA -&gt; ActivityB -&gt; ActivityC -&gt; ActivityD
  </code></pre>
  
  <p>If Android kills the process and the user returns to the application, Android recreates the process and creates ActivityD.</p>
  
  <p>If I kill the process, Android recreates the process and creates ActivityC.</p>