My android app uses AsyncTask to download some data from a website.
But when I press the back button of my android device immediately after the activity starts, worker thread's onPostExecute method is called, which is wierd because android called onDestroy method prior to onPostExecute and the onPostExecute method runs on the main UIThread which I think doesn`t exist anymore.
Can anyone help me what I don`t understand?
An AsyncTask is basically executing what you want in the background as a separate thread from the UI. So when you quit the UI this doesn't necessarily mean that you've killed the AsyncTask. It will continue it's regular life cycle and end in onPostExecute. If you want to kill the AsyncTask too then you will have to call the cancel() function for the AsyncTask.
Know this though, you cannot actually kill an AsyncTask this will be done by Android itself. So you will have to wait a while till the current task is killed (if you call cancel()) for you to restart this particular AsyncTask.
You should also read up on onCancelled() methods. For more information checked out the documentation.
If I've made any mistakes, please correct me.
Related
I have an activity in which I do server sync with a back end server using a subclass of asyctask.
Given it is a network task and might take couple of seconds. I am afraid of the following scenario to take place.
The activity starts, and whenever the asynctask should start to run, it does so.
The onPrexecute() is called, executed, and over. Than the doInBackground() is called, and is done so, however, just when the method is being executed, the user presses the home button and swipes the app from the RECENT APPS. (this ofcourse causes the app to terminate and all the onDestroy methods get called of the alive activities..(Correct me if I'm wrong on this one)).
In my onPostExecute() method, I am inserting the data to DB and updating the VIEWs.
But since the app is 'terminated' the onPostExecute() method never runs.
my questions are :
When the user presses the home button and gets out of the app and swipes the app, is doInBackground halted at that moment ? that is, it is cut in the middle and does not continue what it does ?
What happens to the data that I was going to get from the server and put inside the DB ? Is it advisable to do put the data in the db inside the doInBackground ?
AsyncTask is a background task it will continue to run even if the app is closed and onDestroy() is called. The problem would be when the task enters onPostExecute() and tries to update any views associated with the activity it is bound to, as that activity no longer exists. If you're concerned about this, one thing I've done is to keep a reference to that AsyncTask in the calling activity, and then call the method myAsyncTaskReference.cancel(true) to cancel it in the onDestroy() of the calling activity.
Yes, I would put the DB operations in the doInBackground() method, as that runs in the background on a separate thread, and doesn't require a reference to the app activity.
Have you considered using a service for this type of thing instead? I would strongly recommend an IntentService, which is a subclass of service which is specifically designed to perform a single task in the background (similar to AsyncTask), and then kill itself once completed. It's quite easy to implement and usually only involves overriding one method - onHandleIntent() - which is called when the service starts.
It can handle all your DB operations and it has no reference to an activity and so the problem you're worried about in #1 would never occur. If you need to update a view, you can have your IntentService generate a broadcast once it's completed, and your Activity can register for that broadcast and update it's views accordingly when it receives it. And if your activity is no longer around once the broadcast is sent then it doesn't matter and nothing will fail.
When user presses 'Home', your Activtiy will pause but doInBackground will NOT, but may or may not terminate by system when system feels like it. Activity's onPause will be called. Your Asynctask doInBackGround will NOT halt. It will continue to run until the system kills your App process.
Yes, Db operations can take long. Its advisable to put in doInBackground because it runs on another Thread. onpre/onpostexcute runs on the main thread. If you are worried that System may terminate half way of your db operations, you shouse set Transcation, and only when you are done, you called commit.
Check out this post, I have tested it.
no, it doesn't stop.
It is relly better to put it to datastorage of some kind and then work with it
It is always better to use service for such goals. AsyncTasks just don't fit here. Ask your service to do the work, and then you may start or quit activities as you wish to.
If swiping app from recent stack, it is equivalent to close the app hence it will kill all tasks related to the process so async task will also get killed by the android system. ( even intent service is also get killed)
It is device dependent also. Manufacturers customised removing app from recents behaviour
I have a launcher style app from within which you can launch another app. While that app is launched, I want my app to make note of some things, basically record time etc.
So I have a segment of code whose structure basically looks like this:
appSelectedListener{
runSelectedApp;
private class Background extends AsyncTask<Void, Void, Void>{
protected void doInBackground(){
//executable code
}
}
}
Something alone those lines, I'm sorry if the code is off but I don't actually have anything written yet I just want to get a handle on this stuff first. My question is, when the selected app is brought to the foreground, my app goes to the background. Does the executable code in doInBackground() still get run though? Or would my app necessarily have to be in the foreground?
Does the executable code in doInBackground() still get run though?
Usually. The behavior of threads you fork is not immediately affected by foreground/background considerations.
Eventually, the process for your backgrounded app will be terminated, to free up memory for other apps, or because the user requested that the process be terminated. At that point, all background threads (and everything else you have in RAM) go away. If you have an AsyncTask that has not yet gotten to doInBackground(), doInBackground() will not be called in this case.
To clarify: it's a bit of a misnomer to say that doInBackground() "runs in the background". It runs on a separate thread from the UI thread, but in the same process as the rest of your app (except for any components that you run in a separate process). The UI thread is the highest priority, and the Android system takes steps to ensure that other threads and processes don't interfere with it.
When an event causes a new activity's UI thread to take priority, all other UI threads pause. Other non-UI threads may or may not continue, depending on various factors.
Also, remember that when the user changes the device's orientation, Android's default behavior is to call the onDestroy() followed the onCreate() methods for the activity. This will destroy the AsyncTask object, which destroys the non-UI thread being used to do work asynchronously, which in effect cancels the execution of the code in doInBackground().
For this reason, you should use AsyncTask to do short operations that you don't mind repeating if they get cancelled before finishing. Long-running operations (such as downloading from a server) should be done by an IntentService or sync adapter.
If I have an AsyncTask running indefinitely and Android decides it needs to kill the task either to free up memory or because the device is going into sleep mode (the user presses the power button), is there some way within the AsyncTask to get a notice from the OS that it is about to be killed off, so that action can be taken to complete any outstanding task?
To give you a short answer:
an AsyncTask lives as long as the process it is tied to lives. The OS will never kill an AsyncTask if its process is still running (even in background).
Also please note that the AsyncTask runs independently from the Activity that started it. The Activity may be destroyed and the AsyncTask still lives until the process is destroyed.
EDIT
You need to call the isCancelled() inside the doInBackground() method in order to make sure that when the AsyncTask is destroyed by the OS, the doInBackground finishes so that the onCancelled() method is executed.
So to sum it up: in the doInBackground method always check the isCancelled boolean function. If it returns true, then return from the doInBackground. Automatically, the onCancelled function will be executed instead of onPostExecute. There you may place the code you want to execute prior to its death.
I have a code placed in the onActivityResult() but it is slowing my activity alot,
the code is to do Binarizng on Image.
I am confused whats to use AsyncTask or Thread,
can you help me with it
onActivityResult has nothing to do with threads or async tasks.
A thread allows your app to do two things at once. Doing so can be fairly risky (what if they write the same variables at the same time?). You need to be careful when using them.
An AsyncTask is a Thread that will automatically run some code on the main UI thread when its done. Its useful for running some code on a thread and then synchronizing results on the main thread.
onActivityResult is called when you are running a completely separate activity, either in your own app or in another person's. If you need to run another activity, this is the only way to get a result back from it. It has nothing to do with threads.
Use AsyncTask
AsyncTask Also uses Thread, but in a intelligent way. In android you can not update UI directly from a worker thread. But you can perform UI tasks from Asynctask. So this is a better option.
Both AsyncTask and Thread, let you run background task on a different thread.
But AsyncTask provide you various callback, so that you can Update UI once the background task is done Or Canceled.
So, if your task involve any UI update; please use AsyncTask.
I have an Android app which uses a service to continuously update a textview in the view when the app is open. It updates it using a broadcast receiver which it sends intents to. By the nature of the app, the service needs to continue to run even after the app has exited/the view is gone. Obviously it's not wise to run the service on the main thread in this case, as that will hog up too much and eventually force close and crash the app. So what is the best way to keep this service running? An ASyncTask? A separate thread? How would I go about doing this? I tried it with an ASyncTask like this:
private class MyTask extends AsyncTask {
#Override
protected Object doInBackground(Object... objects) {
return getActivity().getApplicationContext().startService(intent);
}
#Override
protected void onCancelled() {
getActivity().getApplicationContext().stopService(intent);
super.onCancelled();
}
}
Oh yeah, I'm using getActivity().getApplicationContext() because this is inside of a fragment.
So this works fine; I call MyTask.execute() when I want to start it and MyTask.cancel(true) when I want to stop it, but I'm pretty sure I'm not using it how it should be used. I don't even know what I'm supposed to use as the argument for the execute method (??). And it doesn't seem to be the best idea to just start a service in it and nothing else, surely that would work better using just a thread? What's the proper way to go about this, in order for it to work the way it was intended to? By the way, although this does work for much longer than running it on the main thread, it does still crash after several hours.
You can use async tasks to do background tasks in android. Thread handling on your own is ill-advised unless in specific circumstances. The async task will run in a background thread even the user switches to another view and you can get callbacks at periodic intervals using onProgress() update too. Here are some good tutorials on async tasks to get you started. Please go through them carefully since async tasks will be help you a lot in android development.
http://mobileorchard.com/android-app-developmentthreading-part-2-async-tasks/
http://www.vogella.com/articles/AndroidPerformance/article.html
here are the official docs:
https://developer.android.com/reference/android/os/AsyncTask.html
Update: I think I figured it out. I was way off in what I thought I needed to do, apparently for the kind of service I had I only needed to run it in the foreground (so using the startForeground() method). It seems to be working now. Sorry!
You should not start service in another thread than event thread, instead if some code needs longer time to execute in the service, put that code in another thread in Service Class only.
To execute Service in some other process than the main application, you should define process tag in manifest file, in the service.
Calling startService on background thred does not make the service run on a background thread.
You should call startService normally, without using Async Task.
In your service you should create a new thread to do the work. Here you should not use AsyncTask because this service can run indefinitely, it is not a defined task.