AsyncTask status in Activity's onStop() state - android

I have started a AsyncTask to decode the contents in a file on a Button click. AsyncTask is running fine, now I pressed home button. Activity entered into onStop() state. Now what happens to my AsyncTask, Will it run or stop in this onStop() state?

activity stop will not make your async task to stop. Your async task will continue and if you have some reference of view in post execute it will cause exception.
You can also use AsyncTaskLoader instead.
Read about it:
http://mobile.dzone.com/articles/loaders-versus-asynctask

No it can't be. Your activity has been stopped your AsyncTask will continues. IF you want to stop it you have to cancel your AsyncTask.

Related

What happens to running AsyncTasks when the Activity changes?

While Network Operation is running in Asynctask, If user press the Back button and switch to another activity what will happen to Asynctask which is running in background?
AsyncTask Process automatically Kill by OS?
Async Task complete it's entire operation?
If you start an AsyncTask inside an Activity and you rotate the device,the Activity will be destroyed and a new instance will be created.
Similarly if user navigate to another activity,current activity will be destroyed or go in background activity stack and new activity would be in foreground.
But the AsyncTask will not die. It will go on living until it completes.
And when it completes, the AsyncTask won't update the UI of the new Activity. Indeed it updates the former instance of the activity that is not displayed anymore. This can lead to an Exception of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity.
AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. AsyncTask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.
1. AsyncTask processes are not automatically killed by the OS. AsyncTask processes run in the background and is responsible for finishing it's own job in any case. You can cancel your AsycnTask by calling cancel(true) method. This will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object) method is called instead of onPostExecute() after doInBackground() returns.
2. After completion of it's operation, the background thread it's working on is stopped. AsyncTask has an onPostExecute() which is called once your work is finished. This method is called after doInBackground() method completes processing. Result from doInBackground() is passed to this method.
AsyncTask will still run and try to post result on Zombie Activity. Best is to user AsyncTaskLoader.

prevent aynctask getting killed before completion?

in my activity, i have an asynctask that downloads a series of images...(it may take some time depending on the size of the images)... before i execute the asynctask, i display a progress dialog in onPreExecute and a notification (when user clicks on notification it is redirected to my activity with the progress dialog). when my asynctask completes, in onPostExecute, i remove the dialog and the notification.. i handle screen orientation by using onRetainNonConfigurationInstanceto save my asynctask so that when screen is rotated, i check if task is still running and i display the progress dialog if it is still running in onCreate
Problem : sometimes: my asynctask downloads only 1 file and it seems that it gets killed...(no exception in logcat)... as a result, my notication is always there and my progress dialog also... and they remain there indefinitely....
i have tried the solution by Jeff Axelrod there: How can I ensure an AsyncTask is completed before my activity is killed?:
It looks like if I override the onPause() event in my activity, and from within the overridden onPause(), I call cancel(false) on the AsyncTasks, then the activity is not destroyed until the AsyncTasks are completed.
seems to do the trick but problem is that my onPostExecute is not called anymore; all images download fine but as onPostExecute is not executed, notification and progress dialog still remain there forever.
waiting for your solutions guys! i read use asyntask only for short task; will the use of thread and handler solves my problem? will android kills my activity or thread if the latter is not finished??
Best way how to handle Asynctask is described in this article.
In short, the idea is to keep AsyncTask in fragment with setRetainInstance(true); these will keep You AsyncTask alive all time user is in activity holding this fragment and won't be destroyed on configuration change (orientation change).
If You need Your AsyncTask to run after user leaves Activity, for example goes to next Activity but You wish download to continue You should use services.

Logging in Activity, postOnExecute, Resume

I need to implement auto-login process from an activity. Also I need to show ProgressDialog while logging in. I use AsyncTask for login process, because I haven't succeeded to show ProgressDialog in other way, and I execute the mentioned AsyncTask in onCreate. I read that all the code which is executed after doInBackground must be written in onPostExecute, but I have code in onResume. The onResume is launched when AsyncTask hasn't finished its execution yet. Is it possible to launch onResume only after AsyncTask is finished? Or is it possible to execute other functions after AsyncTask is finished (in general)?
Is it possible to launch onResume only after AsyncTask is finished?
No, you need to follow the Activity Lifecycle. And the AsyncTask is asynchronous so that it can run while your UI can still do other things. What you can do is put the code that is in onResume() that you want to run after the task has finished in your onPostExecute() method. There are other options but without seeing the code you are trying to run, this would be my suggestion.
Or is it possible to execute other functions after AsyncTask is finished (in general)?
Yes. You can put that code in another Activity method and call that from onPostExecute()
You also can use getStatus() method of AsyncTask to run code based on the Status of your AsyncTask
Short Answer: No.
If you check the developer android website or this image, you'll check that OnResume() after the Activity is loaded, either after created, paused or restarted. In my case, I have that verification done in OnResume().

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.

How to end AsyncTask onBackPress() and also does it still run if it isnt finished?

I use AsyncTask in several activities.
i know how to Override onBackPress().
But when an AsyncTask is running i want to give the user the ability to go back even though the asynctask is still running.
How can I do this?
Also does an asynctask still run when activity that initiated it is closed before it finishes?
AsyncTasks run on until they complete or are cancelled. So in your case (the user presses Back and you end the main activity), the AsyncTask will carry on.
If you did want to end the AsyncTask from onBackPressed, the best you can do is to call
myAsyncTask.cancel(true);
but all that does is call the task's onCancelled() which can set a cancel flag that can be checked (usually) in doInBackground() - and if it is set the task can return.
Note that using cancel(true) does not stop the task. The docs talk about "interrupting" the task for the true case, but all they mean is that onCancelled will be called while doInBackground is running - at which point the task has an opportunity to set a flag that doInBackground can inspect.
For the true case, if the task does not implement onCancelled, and/or it does not set/inspect a flag, the task will complete doInBackground, but onPostExecute(Object) is never invoked - which is equivalent to the false case.
The async task will continue to run even if your application was closed. You have to be careful with that since your task will leak your context (keep it in memory) if it has a reference to it (so the activity) as long as your task is still running. You can avoid that by referencing your activity by a WeakReference.
You can stop a running task with cancel(true). A cancel will let the task finish its doInBackground but will never call onPostExecute. You could interrupt your background routine by checking isCanceled() and so return earlier since the task was killed.
I deleted one half of my post!
I did so confuse interrupt with a thread stop! Using true will of course NOT stop the actual thread. Thanks a lot to Torid and his answer which proved me wrong! Big lesson lernt, thanks for that!

Categories

Resources