Wednesday, April 24, 2013

[android help] AsyncTask will always run even if app is destroyed?

android - AsyncTask will always run even if app is destroyed? - Stack Overflow








Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















i have an application and because you cant do network operations on the main thread im using AsyncTask, so the question is once i execute() the AsyncTask right after that i finish() the activity, and maybe the user will finish() the whole app, so what im wondering is:


  1. will AsyncTask always finish doInBackground() and onPostExecute() even if the app is closed as long as execute() was called when the app was running?

























You will be able to test this. And yes It does. If execute was called you can see Asynctask will still execute UNLESS it does something to the forground or UI related. (it may cause launcher to crash).



However, if it was close by the system. It may or may not continue executing the method. I have already tested and answered here.























onPostExecute() is called from the UI thread - so if the UI thread is no longer running, it will not continue to run. However doInBackGround() is ran from a separate worker thread so it will keep on until done (or if the JVM process is killed by the OS, which is also a possibility). Note that AsyncTasks are only recommended for shorter UI-bound background tasks and not long-running background work (a few seconds).


In short, you can not assume that it will keep on and definitely not assume that it will post it's progress or the calling of onPostExecute().




















default







.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...