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.
I know how to kill my process. That isn’t the problem. The problem is that when I kill my process (using DDMS, adb shell kill
, Process.killProcess()
, etc.) Android does not restart it the same way that it would if the Android OS had killed it itself.
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 top activity on the activity stack (calling onCreate()
).
On the other hand, if I kill the process, Android assumes that the activity on the top of the activity stack was badly behaved, so it automatically recreates the process and then removes the top activity from the activity stack and recreates the activity that was underneath the top activity (calling onCreate()`). This is not the behaviour I want. I want the same behaviour as when Android kills the process.
Just to explain pictorially, if my activity stack looks like this:
ActivityA -> ActivityB -> ActivityC -> ActivityD
If Android kills the process and the user returns to the application, Android recreates the process and creates ActivityD.
If I kill the process, Android recreates the process and creates ActivityC.