我的 android 应用程序使用 AsyncTask 从网站下载一些数据。
但是当我在活动开始后立即按下我的android设备的后退按钮时,工作线程的onPostExecute方法被调用,这很奇怪,因为android在onPostExecute之前调用了onDestroy方法并且该onPostExecute
方法在我认为不存在的主UIThread上运行了。
谁能帮助我我不明白的事情?
我的 android 应用程序使用 AsyncTask 从网站下载一些数据。
但是当我在活动开始后立即按下我的android设备的后退按钮时,工作线程的onPostExecute方法被调用,这很奇怪,因为android在onPostExecute之前调用了onDestroy方法并且该onPostExecute
方法在我认为不存在的主UIThread上运行了。
谁能帮助我我不明白的事情?
An AsyncTask
is basically executing what you want in the background as a separate thread from the UI. So when you quit the UI this doesn't necessarily mean that you've killed the AsyncTask. It will continue it's regular life cycle and end in onPostExecute
. If you want to kill the AsyncTask
too then you will have to call the cancel()
function for the AsyncTask
.
Know this though, you cannot actually kill an AsyncTask
this will be done by Android itself. So you will have to wait a while till the current task is killed (if you call cancel()
) for you to restart this particular AsyncTask
.
You should also read up on onCancelled()
methods. For more information checked out the documentation.
If I've made any mistakes, please correct me.