Greg
Greg

Reputation: 444

Cancel AsyncTask on app exit SOVLED

I seem to have a problem with my application. What's going on is I want to kill my Asynchronous Task when the user closes the program. My reasoning behind this is that I have a StopWatch that runs off an AsyncTask, and when the user closes the program, the Task continues to run and takes up memory. I would like to cancel the task using this method:

 Task.cancel(true);

But I don't know when to make the call in my activity. How would I do so? Here is the task I am running:

 public class timer extends AsyncTask<String, String, String> {

    @Override               
protected String doInBackground(String... params) {
    while(true){    
    while(milli < 1000 && running){ //running and other vars are arbitrary variables
            running = true;
            milli++;
            try{                
                Thread.sleep(1);                
            }
            catch(Exception e1){
                System.out.println(e1);
            }
            if(milli == 1000){
                milli = 0;
                secs++;         
            }
            if(secs == 60){
                secs = 0;
                mins++;             
            }
            if(mins == 60){
                hours++;
                mins = 0;
            }                       
            this.publishProgress(milli+":"+secs+":"+mins+":"+hours);            }               
        }
    return null;
    }   
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        TextView display = (TextView) findViewById(R.id.textView1);
                 display.setText(values[0]);
    }
    }
#####SOLUTION################SOLUTION

I kind of took a bit from everyone, but I ultimately figured it out myself. I overwrote the onStop method of activity like so:

protected void onStop(){
super.onStop();
Task.cancel(true);
}

Thanks to all the contributors!

#####SOLUTION################SOLUTION

Upvotes: 1

Views: 4831

Answers (3)

Kartheek
Kartheek

Reputation: 7214

If you want stop the async task you can use asyncTask.cancel(true/false); true specifies the android system to stop this service even it had already started, false specifies the android system not to stop this task if it has already started. if you want stop this service when the activity looses its interaction write it in onPause() or if you want to cancel at the time of activity destroy call it in onDestroy() for more detail check this

Upvotes: 2

Dan
Dan

Reputation: 330

Have a look at the Activity lifecycle. There is a nice diagram on this link.

You want to start your timer in onStart()/onResume() and stop it in onStop()/onPause().

Note that you need to choose a consistent pair onStart/onStop or onResume/onPause to get a consistent behaviour.

Upvotes: 2

GhostDerfel
GhostDerfel

Reputation: 1551

You can achieve this checking the application process info and adding a boolean in your while so the Async Task can be killed.

To get your app info try with this line of code:

ActivityManager.getRunningAppProcesses()

Check more here: http://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo.html

If the app is running on foreground you kill your Async Task with this lines of code:

myAsyncTask.cancel(true);

Upvotes: 0

Related Questions