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

后端 未结 6 1514
你的背包
你的背包 2021-02-18 15:05

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

    protected void onPr         


        
相关标签:
6条回答
  • 2021-02-18 15:25

    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 讨论(0)
  • 2021-02-18 15:31

    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 :).

    0 讨论(0)
  • 2021-02-18 15:32

    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.

    0 讨论(0)
  • 2021-02-18 15:38

    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.

    0 讨论(0)
  • 2021-02-18 15:44

    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.

    0 讨论(0)
  • 2021-02-18 15:48

    It keeps running until the onPostExecute finishes.

    0 讨论(0)
提交回复
热议问题