15

What happens on Activity.finish() with an AsyncTask still running in background?

Does it just pop the Activity off the Activity Stack, but wait to destroy the Activity object until the AsyncTask fully completes (since the AsyncTask is an inner class of my Activity)?

Also, would it act any differently if the AsyncTask were a public, non-inner class that held no references to the instance of the Activity?

stormin986
  • 7,672
  • 15
  • 42
  • 54

1 Answers1

11

I've tried the same thing with the Thread, and my observation is it keeps running the thread.

Karan
  • 12,724
  • 6
  • 40
  • 33
  • 17
    Yes, the `AsyncTask` will keep running to completion. It will hold onto a reference to the `Activity`, keeping the `Activity` from being garbage-collected...unless it is a static inner class or fully independent class that has no reference to the `Activity` or anything that points to the `Activity` (e.g., a `View`). – CommonsWare Apr 28 '10 at 11:54
  • @CommonsWare but then, what happens if one of the `AsyncTask`'s methods that runs on the UI thread is invoked and it manipulates UI views of the Activity, invoking Activity's methods like `findViewById(R.id.something_in_the_activity_layout)`? Does it still work or does it silently fail? – Gianni Costanzi Aug 24 '12 at 21:27
  • @GianniCostanzi: That is impossible to answer in the abstract, sorry. – CommonsWare Aug 24 '12 at 21:30
  • @CommonsWare I tried to better explain my question in a new question [here](http://stackoverflow.com/questions/12117031/what-happens-to-an-asynctask-when-the-launching-activity-is-stopped-destroyed-wh) – Gianni Costanzi Aug 24 '12 at 21:42
  • @Gianni Did you open a new question as per your last comment. The discussion was going into an interesting edge but no comments after that ! – Gem Mar 09 '15 at 18:24