Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping Background Processing (AsyncTask) when user leaves the application

Tags:

android

I have developed an android 2.1 application. In that I am using AsyncTask for doing background processing. It gets the data from the WebService.

The Problem is : When I leave the application, the background service remains active only. When I go to Settings->Applications->Running Applications it shows me that the application is running. I have to forcestop that application.

Is there any way to stop the background tasks automatically when user leaves the application ?

like image 318
Kartik Domadiya Avatar asked Jan 28 '26 16:01

Kartik Domadiya


1 Answers

AsyncTask has a cancel method (http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean) )

Invoke it when your Activity is going to stopped (onStop) state, or maybe paused - onPause depending on your needs, see Activity lifecycle for further explanation here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

like image 171
Olegas Avatar answered Jan 31 '26 07:01

Olegas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!