I am wondering: when an app is to be killed, does Android wait for the currently running function to return, or does Android stop it before it ends by itself?
-
That'd be an endless waiting game. The function that is being executed is somewhere halfway another function. It'd essentially have to wait till the program is done, which beats the purpose of killing. – Jeroen Sep 01 '15 at 19:09
7 Answers
Updated: After testing, Android will kill any thread/AsynTask that is currently running(without calling onPostExecute()/onCancelled());
If you close Application normally. Your ui-thread stops, but If you implment your method in a Thread / AsyncTask / Service, it will continue to run until it finishes.
However, your Thread / AsyncTask may continue running but may or may not to be fully functional if you have a call back of your instance of the application or do something to the ui of the application. This MAY give you an Exception depending on what is being done.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread(new Runnable() {
@Override
public void run() {
try {
int i = 0;
while (true) {
i++;
Thread.sleep(2000);
Log.i("not dead", "not dead" + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
Ran this code, and go play some games. Results were the thread will killed by the System.

- 18,603
- 10
- 49
- 67
-
Not sure that's the case when Android kills an app. When the user leaves it, yes, but if Android kills it (to free memory for example) I think it nukes the whole thing. I don't know what happens to methods in progress though, I'd like to know. – Khantahr Jan 30 '13 at 19:18
-
1I will update answer once I test it, but battery will die out int 2mins. – wtsang02 Jan 30 '13 at 19:30
The OS kills the JVM process, with all the "functions" in all the threads. In practical terms, you shouldn't assume that a method is a transaction. Instead, one should assume it can be terminated at any time and design appropriately.

- 1,747
- 2
- 11
- 13
-
1What happens to wake locks? Does Android release any wake locks held by an application when it kills it? – Khantahr Jan 30 '13 at 19:32
-
1@Ralgha: Hard to find anything in the documentation. Speculating, what options would an OS have if it didn't provide applications with a robust termination hook? What if some applications failed to clean up after themselves? One option is the cleanup by the OS. What are the others? – full.stack.ex Jan 30 '13 at 21:22
Your app may be killed by the system at any time without another line of its code being executed. According to the documentation however, there are a few methods which are not "killable":
- onCreate()
- onRestart()
- onStart()
- onResume()
- onPause()
As onDestroy() won't get called for sure, it is more save to use the onPause() Method to write any persistent data to storage:
protected void onPause(){
super.onPause();
if(isFinishing()){
// store data
}
}

- 4,403
- 4
- 42
- 54
It would make sense that it would kill a method mid-execution. Consider that many "hangs" are due to an infinite loop condition within a single method's loop. How else would an OS kill such an app without killing it in the middle of executing a method?
Also consider this: Even if an OS allowed the "current" method to finish executing before killing the app, and then killed the app, it would then be killing the app in the middle of the method that had called the first method that the OS allowed to continue executing until it finished. OK, so then continue this thinking to its extreme. If the OS allowed each function that was in the middle of executing to finish before killing the app, then the end result is that the OS allows the entire app to run to completion, which is exactly what killing an app is supposed to circumvent. Thus, an OS, when killing an app, must terminate it in the middle of "a"... acutally "many".... functions. Every function that's on the callstack does NOT get a chance to finish!

- 7,701
- 3
- 31
- 55
Android can kill your app at any time, at any point. But android tries to keep your app running if he has enought resources for it or if you have foreground activity or service.
Before killing your app android will call lifecycle methods of your components according SDK documention. Not all lifecycle methods are guaranteed to be called.

- 10,482
- 2
- 28
- 37
Each android app runs on a separate Dalvik VM. and when android kills that app , it destroys that VM instance. and doesn't care for the threads you are using or what are they doing , you can easily test that , by creating a tcp server socket which will block until a client connects and then kill the app, nothing happens, no interrupt exception raise or anything because the VM instance itself get killed.

- 9,192
- 5
- 39
- 51
It really depends on the method of killing. Killing due to crash? All threads are stopped where ever they are. Killing due to watchdog timer? All thread killed wherever they are. Killed due to closing the activity by user? Activities/threads will finish what they're doing. Basically, you can't count on it, but it may happen. If you're worried about data corruption due to closing, you need to use a transactional model.

- 90,003
- 9
- 87
- 127