What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running?

前端 未结 2 480
春和景丽
春和景丽 2020-12-16 02:55

I\'ve seen few questions nearly identical to mine, but I couldn\'t find a complete answer that satisfies all my doubts.. so here I am.. Suppose that you have an activity wit

2条回答
  •  长情又很酷
    2020-12-16 03:10

    The doInBackground() will keep on running even if your Activity gets destroyed(i,e your main thread gets destroyed) because the doInBackground() method runs on the worker's/background thread. There will be a 'problem' in running the onPostExecute() method as it runs on the main/UI thread and you may experience running into unrelated data but there will be no exception shown to the user. Thus, it is always better to cancel your AsyncTask when your activity gets destroyed as there is no reason to run AsyncTask when the Activity is no longer present. Use android Service if you continuously want to download something from the network even when your Component/Activity gets destroyed. Thanks.

提交回复
热议问题