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?
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.