1

I'm having some trouble with an AsyncTask subclass.

I have a main activity as below that displays a button and a number that counts up on the screen. Clicking the button launches an Edit activity where a number can be entered.

The number displayed on the Main activity should update with the timer which it does but the trouble I'm having is that I can't stop the timer. It should stop when entering the Edit activity and returning from it as well, as well as restart with the a new value too but it doesn't, the timer is always running with the first entered value, it never stops, even when I leave the program and return to the home screen.

I've looked at posts here such as Can't cancel Async task in android but they all just mention checking for isCancelled() which I'm doing. Can anyone see/explain why I can't stop this AsyncTask ?

public class MainActivity extends Activity {

@Override
UpdateTimer ut;
TextView tvn;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onResume() {
    super.onResume();

    tvn = (TextView) findViewById(R.id.numDisplay);

    if(ut != null )//&& ut.getStatus() == AsyncTask.Status.RUNNING) {
        ut.cancel(true);
        ut.cancelled = true;
        Log.d("-----M_r","called cancel: "+ut.isCancelled()+" "+cancelled);
    }

    if (updateRequired) {
        ut = new UpdateTimer();
        ut.execute(number);
        updateRequired = false;
    }
}

public void onEditButtonPressed(View caller) {

    // kill any running timer
    if(ut != null )
    {
        ut.cancel(true);
        ut.cancelled = true;
    }

    // start the edit screen
    Intent e_intent = new Intent(this, EditActivity.class);
    startActivity(e_intent);
}

private void updateScreen(long number) {

    // update screen with current values
    tvn.setText("" + number);
}

private class UpdateTimer extends AsyncTask<Long, Long, Integer> {

    long number;
    public boolean cancelled;

    @Override
    protected Integer doInBackground(Long... params) {

        number = params[0];
        cancelled = false;

        while(true) {
            number += 1;

            //sleep for 1 second
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // tell this AsyncTask to update the time on screen
            publishProgress(number);

            // check if timer needs to stop
            if (isCancelled()) break;
            if(cancelled) break;
        }

        return null;
    }

    protected void onProgressUpdate(Long... progress) {
            Log.d("-----M_ut","updated: "+number+"  "+this.isCancelled()+" "+cancelled);
        updateScreen(progress[0]);
    }

     protected void onCancelled(Integer result) {
         cancelled = true;
         Log.d("-----M_ut","-- cancelled called: "+this.isCancelled());
    }

}

protected void onStop()
{
    super.onStop();

    // kill any running timer
    if(ut != null) {
        ut.cancel(true);
    }
}

}

Community
  • 1
  • 1
aussie oni
  • 11
  • 3

2 Answers2

0

Try this... remove the variable..cancelled and change to this..

@Override
protected void onCancelled() {
    super.onCancelled();
}

call the super.onCancelled instead..

And in the doInBackground check

if (isCancelled()) {
    break;
}

Try calling from your activity ut.cancel(true);

Hope it works:)

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • Thanks for the suggestion but it didn't work. I forgot to mention before that the log lines all return true regarding isCancelled except for the one in onProgressUpdate. – aussie oni Dec 23 '13 at 11:35
0
private YourAsyncTask ut;

declare your asyncTask in your activity.

ut = new YourAsyncTask().execute();

instantiate it like this.

ut.cancel(true);

kill/cancel it like this.

amalBit
  • 12,041
  • 6
  • 77
  • 94
  • Thanks, but this didn't work. I can't see why isCancelled returns true from everywhere else except doInBackground and onProgressUpdate. – aussie oni Dec 23 '13 at 11:57