开发者

If an activity is killed, does the AsyncTask live on?

开发者 https://www.devze.com 2023-02-15 14:59 出处:网络
I think I know the answer to this, but does an AsyncTask continue to live on once its calling Activity has been 开发者_C百科finish()ed?

I think I know the answer to this, but does an AsyncTask continue to live on once its calling Activity has been 开发者_C百科finish()ed?

    protected void onPreExecute() {
        Toast.makeText(getApplicationContext(), "Your data is processing.", Toast.LENGTH_LONG);
        finish();
    }

Edit: so far two different answers :)


The AsyncTask is tied to a UI thread and if the Activity is finished the async task is canceled.

[update] - Hackbod's comment below is correct. It should be noted that AsyncTasks are meant to be short lived and as such not worry so much about this issue. An AsycTask is only truly gone when it is completed OR the process is killed which may or may not happen after finish is called.


It keeps running until the onPostExecute finishes.


I've experienced some weirdness with Async Tasks where if an activity is killed by the OS and when the app resumes, the AsyncTasks sometimes do not run at all. I am unsure why they get into this state. If you execute your AsyncTasks in your own executor thread, this does not happen. I might be running into some odd circumstance. Thought it might be useful to post here anyway :).


Async task keeps running untill he onPostExecute() is finished.

try it by putting a toast on finish() and onPostExecute() to see which toast is shown first.


Asynctask are not bounded to any life cycle method of Activity or Service.

It keeps running in a separate thread until the onPostExecute() gets executed in the main thread.


Async Tasks have no effect of the Activity lifecycle or any configuration changes. They keep on running till onPostExecute() method has been called.

One should use Services for long running operations instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号