Android - Simple AsyncTask - android

Hi I have a question about Android AsyncTask.
I have a fragment within an Activity.
That fragment calls AsyncTask where it does something heavy and update the UI in the onPostExecute().
My question is this. LEt's say the user opened the Activity.
Now the fragment is attached and AsyncTask's doInBackground() is called.
Then the user suddenly presses back button to close the activity.
Now, what happens to the execution in the AsyncTask thread?
Does thread also die as soon as the activity is closed?
Or do I have manually check in onPostExecute whether this Fragment is still attached by using isAdded() at the beginning of onostExecute()?
Thanks!
EDIT
In my Fragment, I declare AsyncTask() implicitly.. like this
new AsyncTask<String, Void, Boolean>() {
#Override
protected Boolean doInBackground(String... params) {
return processPlays(plays);
}
#Override
protected void onPostExecute(Boolean result) {
if (!isAdded()) return; // IS THIS FINE?
// UPDATE UI
}
}.execute();

Threads run until they either finish or until Android system dumps the memory. Your AsyncTask will keep running, so you need to do the check if you can't outright cancel it. Else you'll get these situations where your app will crash without you even seeing it open.
Likewise, Fragments can be detached and destroyed during the lifecycle of an app. You can never assume that a Fragment or Activity is still active once returned from an AsyncTask's doInBackground()

On that case the AsyncTask thread will keep running until the end, and if unchecked may crash inside onPostExecute(). Most of the times you'll want to cancel the AsyncTask on the onStop(), onFinish() or even onPause() of the Activity (depending on your application).
You can cancel a Task with the cancel(boolean mayInterruptIfRunning) method. From the Android reference:
A task can be cancelled at any time by invoking cancel(boolean).
Invoking this method will cause subsequent calls to isCancelled() to
return true. After invoking this method, onCancelled(Object), instead
of onPostExecute(Object) will be invoked after
doInBackground(Object[]) returns. To ensure that a task is cancelled
as quickly as possible, you should always check the return value of
isCancelled() periodically from doInBackground(Object[]), if possible
(inside a loop for instance.)

Related

onCancelled() doesn't get called by calling cancel() once onPostExecute() get called?

I am trying to dismiss dialog on onCancelled(Boolean aBoolean) of AsyncTask :
1. It will get called if I call 'myAsyncTask.cancel(true)' if AsyncTask is running.
2. It won't get called if AsyncTask already called onPostExecute().
Do anyone has idea?
Actually, AsyncTask.cancel() doesn’t kill the Thread with no regard for the consequences. All it does is set the AsyncTask to a “cancelled” state. It’s up to the developer of AsyncTask to adhere cancel state.
One of the strategies is to check isCancelled() from time to time and act appropriately. onPostExecute you can also read “cancelled” state and call onCancelled() (or any other function with common logic) manually.
In short, You can't not.
When you called .cancel(true) methods, AsyncTask execute onCancelled instead of onPostExecute.
onPostExecute means Background Tasks is over, back to UI Thread.
I think there is new way.
In onPostExecute, just adding boolean isProcessing = true;
when you don't process onPostExecute, just switch isProcessing's value to false. (I'm not sure about that way)
#Override
protected void onPostExecute(Long result) {
if (isProcessing) {
Log.d(TAG, "onPostExecute - " + result);
}
}

Activity restart while async task is running

I'm confused with the async task. What do I have to do when my activity restarts?
In my activity, onCreate() starts an async task.
I know that the activity restarts when android requires it (i.e. orientation change or other).
I've no problem with this... and I think restarting a new async task is acceptable.
Nevertheless I don't know what's happening with my previous async task.
Do I have destroy it ?
My second question: what if I have a progressDialog in my previous task. Do I have to dismiss this dialog (and how)?
No, your Asynctask will end with your activity as well as your progressDialog. When your activity calls onRestart() it must first go through onPause and onStop which will destroy your activity but not your application.
For more information on activities - http://developer.android.com/reference/android/app/Activity.html
Additionally, it would be safer to cancel your Asynctask as well as set progressDialog to null.
From Asynctask documentation
A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
you have to save the instance of activity and when your activity restart you have to resume your activity with that instance use this link: Saving some data on orientation change in Android
ya you have to dissmis that dialog.

Android Async task cancel/stop

I want to know what is the best way to stop an async task frm running.
I have tried
#Override protected void onCancelled() {
super.onCancelled();
mTask.cancel(true);
}
I also tried
asyncTaskObject.cancel(true);
This works specially when associated with an event.
But suppose the scenario is--- there are 4 AsyncTask. First call the second, second calls the third and third calls fourth. When the user enters the activity there is no dialogbox.
Otherwise we could have used the onCancel method there.
When user clicks on anywhere on the page the dialog box appears if user does not click anywhere then no dialog box is shown but async task keep running in the background.Suppose the user clicks the "back" button on or the navigational icon to the home page user.is taken out of the current activity. But the async task keep running in the background and eventually the app crashes. I have used to the cancel method in onBackPressed. But the problem is you cannot be sure which task is running and app carshes again.
What is the way out of this?
keep reference to AsyncTask object as instance variable and then in onDestroy() do this
#Override
protected void onDestroy() {
if (mTask != null) {
mTask.cancel(true);
}
super.onDestroy();
}
In http://developer.android.com/reference/android/os/AsyncTask.html there's a session called Threading rules that say that AsyncTasks instances must be created on the UI thread and execute must be invoked on the UI thread. If you invoke execute from the UI thread you can cancel the thread calling yourTaskInstance.cancel(true);
I am not entirely sure when you want to cancel your tasks, but here are a few suggestions: a) keep a reference to each task that is running. b) add a dismiss listener to your dialog and cancel all tasks there (if that's what you want to do). c) cancel all tasks at the onStop callback of your activity (if that's what you want to do again).

android: cancel(true) does not kill the AsyncTask

I am using an Android AsyncTask in order to download files from a server. when files are downloaded, I am trying to kill the AsyncTask.
protected void onPostExecute(Void result) {
MyTask.cancel(true);
}
But it stills running (I can see it from the Debug window in eclipse).
How to kill the AsyncTask?
When the AsyncTask is finished running, onPostExecute() will be called. The thread will die. The garbage collector will clean it up. You don't have to do anything. What you're doing is redundant.
Other than that, calling "Cancel" sends and interrupt signal to the thread. If your process can be interrupted, it will stop blocking and continue to execute. You then have to call isCancelled() in doInBackground() and return from the method if isCancelled() is true. After which, onCanceled() and onPostExecute() will be called and the thread will die on it's own like normal.
There is some documentation from here about canceling an AsyncTask, which may be relevant:
A task can be cancelled at any time by invoking cancel(boolean).
Invoking this method will cause subsequent calls to isCancelled() to return true.
After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.
To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)"

Android AsyncTask blocks back key dispatch Event

The Problem is:
My Activity starts an AsyncTask in onStart().
In the doInBackground Method I make a short webrequest, and depending on your network connetion, this may take a little, so I want this task cancelable...
BUT..
After hours of searching, testing and debugging I noticed now, when the user presses the back button during the doInBackground Method, the Keyevent ist always dispatched AFTER my doInBackground method is finished.
So I dont have to wonder why the asynctask never is cancelled when the users presses the backbutton,
the AsyncTask.cancel(true) is invoked too late..... (even if I am not sure if cancel(true) will help)
So is this normal behavoiur with asynctask and backbutton?
This cant be normal, because how should the user ever get back from the activity on slow connection?
wait for timeout?
I am Begging for Help, cancel an async webrequest SHOULD be possible :)
when the user presses the back button during the doInBackground Method, the Keyevent is always dispatched AFTER my doInBackground method is finished.
No, this isn't true.
If I hit the BACK button on my device when an AsyncTask is running in my main Activity (downloading files from my server), the Activity is immediately closed.
What IS true, however, is the AsyncTask will continue to run whatever code is in its doInBackground() method UNLESS I explicitly cancel it (but you kind of know that already).
As far as I can tell, your 'webrequest' (whatever that may be) is blocking your doInBackground() method and because of that, any attempt to cancel it in onPause(), onStop, onDestroy() etc will never work.
As advantej points out, the way the AsyncTask.cancel(...) method works is that it causes isCancelled to be set to 'true'. In order to successfully cancel the AsyncTask's doInBackground() method you need to periodically check isCancelled. Example...
#Override
protected Void doInBackground(String... params) {
while (!isCancelled) {
DoSomething();
}
}
The problem is if DoSomething() (for example your 'webrequest') is blocking the while loop then isCancelled won't be checked until it completes.
This is the expected behavior. The documentation says :
Cancelling a task
A task can be cancelled at any time by
invoking cancel(boolean). Invoking
this method will cause subsequent
calls to isCancelled() to return true.
After invoking this method,
onCancelled(Object), instead of
onPostExecute(Object) will be invoked
after doInBackground(Object[])
returns. To ensure that a task is
cancelled as quickly as possible, you
should always check the return value
of isCancelled() periodically from
doInBackground(Object[]), if possible
(inside a loop for instance.)

Categories

Resources