0

I have a small doubt. I have created an asyncTask and in doInBackground i am just printing the number inside a for loop (iterating 100 times). Inside a loop i am storing the value of integer in shared preferences also. My question is why my async task is not exiting when i pressed by back button . And i also checked that onDestroy method is getting called. But still my async Task is printing the numbers and also storing the count in sharedPreferences.

Then if async Task runs in background even after your app is killed then what is the use of service. Because service also shows the same behavior.

  • AsyncTask work on separate thread but your all ui works on UI Thread (main thread). So your separate thread keep running even you back press. You can cancel you async task on backpress See link - https://stackoverflow.com/questions/6039158/android-cancel-async-task – Sanjay Kumar Feb 07 '18 at 14:58
  • then what is the use of service ? It also runs in the background completes it job and then terminates and the same behavior asynctask is showing. Then why services are introduced ? – Developer_vaibhav Feb 07 '18 at 15:01
  • I think aysyn task creates a process instead of thread . So when your app is get killed then it does not kills the process. As process is independent it runs continuously in the background. And when your app is removed from the recent task then android system kills all the process. Can someone please tell me is this what happens? – Developer_vaibhav Feb 07 '18 at 15:13
  • AsyncTask is just suger above common thread. You can just fire up thread and it will be running fine without activity. No new process is created. If you really need to know the diference between service and thread, just read the doc. – egoldx Feb 07 '18 at 15:22

4 Answers4

0

You most certainly have a loop in the doInBackGround method which does not get cancelled by calling Asynctask#cancel. See the docs for it and the way to handle

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.)

Source

So check in your loop for isCancelled() periodically and stop the loop if the value is true.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

The difference between ASyncTask and Service as explained in this post Android: AsyncTask vs Service

AsyncTasks are designed for once-off time-consuming tasks that cannot be run of the UI thread. A common example is fetching/processing data when a button is pressed. Services are designed to be continually running in the background. In the example above of fetching data when a button is pressed, you could start a service, let it fetch the data, and then stop it, but this is inefficient. It is far faster to use an AsyncTask that will run once, return the data, and be done.

Async tasks run on a new thread, every app has a UI(main) thread that you must use to update the app's UI, and if you are doing some work on the main thread that require a lot of time to complete you may get App Not Responding, so that is why you should do all the time and resource consuming work on a separate thread by using AsyncTask. Once started the AsyncTask is not destroyed until you explicitly call cancel() on the object instantiated from the class that extends AsyncTask, so if your activity gets destroyed the AsyncTask will continue working until it completes it's work. If you want to interrupt your AsyncTask when your activity gets destroyed, you should call cancel(boolean val) in activities' onDestroy() method where val if true will interrupt the method doInBackground() in your AsyncTask otherwise if false will let doInBackground() complete its work and in both cases instead of calling on onPostExecute will call onCancelled()

Angel Gruevski
  • 127
  • 1
  • 5
0

The main AsyncTask's purpose is to enable an easy way to work with Threads and Handlers. It creates a background Thread while publishing results to the UI Thread. Watch this Google's Android Course AsyncTask lesson to get more details.

Be aware from some concerns such as Asynctasks should be used for short operations (as mentioned at the documentation).

Through the Google's lesson you will realize what is going on with your code.

-1

Ya. That's a bit confused. AsyncTask runs on a different thread. First of all you need a global variable for your task.

private myTask = new MyAsyncTask();

Whenever you need to cancel you task just call myTask.cancel(true);

Then inside your asynctask you can check for isCancelled() method which returns true if you has canceled your task.

F.e.

while (!isCancelled()) {

....
}

But better use RxJava for such kind of tasks. Its easier to cancel and so on.

TooLazy
  • 836
  • 4
  • 11
  • 29
  • yes this will definitely work. But i am confused if asynctask is good enough to run in the background even if your app is killed. Then what is the use of services.? – Developer_vaibhav Feb 07 '18 at 15:06