AlexVPerl
AlexVPerl

Reputation: 8016

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.

Upvotes: 2

Views: 763

Answers (2)

Stepango
Stepango

Reputation: 4851

AsyncTask based on thread so there is no huge difference in it's behaviour both will run after onDestroy. To avoid this behaviour you could use Loaders or manually stop execution of AsyncTasks/Threads in onDestroy method.

Upvotes: 0

Pasi Matalamäki
Pasi Matalamäki

Reputation: 1853

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

Upvotes: 3

Related Questions