Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't cancel running AsyncTask
    primarykey
    data
    text
    <p>I'm having some trouble with an AsyncTask subclass.</p> <p>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.</p> <p>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.</p> <p>I've looked at posts here such as <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;ved=0CCgQFjAA&amp;url=https://stackoverflow.com/questions/14279695/cant-cancel-async-task-in-android&amp;ei=7-e3UqT7I4jikgWks4H4CQ&amp;usg=AFQjCNH2wCR_MzkUyb4w1e3JC2wcI21HLg&amp;bvm=bv.58187178,d.dGI&amp;cad=rja" rel="nofollow noreferrer">Can't cancel Async task in android</a> but they all just mention checking for isCancelled() which I'm doing. Can anyone see/explain why I can't stop this AsyncTask ?</p> <pre><code>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 )//&amp;&amp; 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&lt;Long, Long, Integer&gt; { 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); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload