Search code examples
javaandroidmultithreadingandroid-asynctaskrunnable

Thread Runnable vs AsyncTask Life Cycle


I would like to better understand what happens to a Thread or a AsyncTask when activity is destroyed.

So in this scenario, a Thread or AsyncTask would be started from activity, and user hits home button and onDestroy gets called triggering GC before either Thread of AsyncTask had a chance to finish.

Which one is more likely to run till completion in this scenario Thread/Runnable or an AsyncTask ?

Thanks.


Solution

  • Both will run

    But the problem is if you have a reference to the killed activity on Thread of AsyncTask it will leak, and that is a problem you need to solve to make long running tasks synchronized with UI

    If you've got a bigger task, I'd suggest you to spin up a Service, which are more easy to handle in Android context