Mubeen Iqbal
Mubeen Iqbal

Reputation: 31

How to stop asynctask during fragment transactions

I am having issue that if AsyncTask is running during fragment transaction then the fragments become white(no content is displayed) so is there any way to stop AsyncTask during fragment transaction and resume once transaction is completed.

Upvotes: 1

Views: 362

Answers (3)

seon
seon

Reputation: 1060

asyncTask in your activity:

private YourAsyncTask aTask;

instanitiate

aTask = new YourAsyncTask().execute();

cancel it like this:

aTask.cancel(true);

Original How to stop asynctask thread in android?

Upvotes: 0

from56
from56

Reputation: 4127

You have to call asyncTask.cancel() and then your code in asyncTask.doInBackground() must test periodically what isCancelled() returns, if true it must return as soon as possible.

Finally when doInBackground returns asyncTask.onCancelled() will be called on the UI thread.

Upvotes: 2

Ajeet Singh
Ajeet Singh

Reputation: 2009

You have to keep the reference of the AysncTask like:

MyAysncTask asyncTask = new MyAysncTask();
asyncTask.execute();

then in onPause() or before calling transaction.commit() call asyncTask.cancel().

Hope it will help!!!

Upvotes: 1

Related Questions