AsyncTask will always run even if app is destroyed?

前端 未结 5 864
孤街浪徒
孤街浪徒 2020-12-31 04:36

I have an application and because you can\'t do network operations on the main thread I\'m using AsyncTask, so the question is once I execute() the

5条回答
  •  难免孤独
    2020-12-31 05:22

    When you call finish() on Activity, the Activity is destroyed, but the main thread is not. [NOTE: Activity runs on Main Thread. Activity is not Main Thread. ]

    So, doInBackground() on background thread and onPostExecute() on main thread will be executed. However, if onPostExecute() does any UI-related tasks, you will get ANR because there is no UI at this point. For eg, if you just print a Log.d() statement inside onPostExecute(), the statement will be visible in Logcat.

    ** These will be possible only if the process is alive, & is not killed by Android Low Memory Killer.

提交回复
热议问题