Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <code>onCancelled</code> is not being called, then your <code>rm</code> method is still running. Because as you mentioned, it's running a loop.</p> <p>The best way to control the process (know if it needs to be stopped) is by polling or tediously checking the status of a <code>volatile boolean</code> variable within your <code>rm</code> method.</p> <p>For example, create a <code>static volatile boolean</code> variable within your AsyncTask class called <code>cancel</code>. Set this variable to <code>false</code> in the <code>onPreExecute</code> method.</p> <p>In your <code>rm</code> method, check to see if <code>cancel</code> is true before and after the heavy tasks (opening a file, reading a file, part of a download loop).</p> <p>If it's true, then break out of the method with a <code>return</code> statement. Better yet, make your <code>rm</code> method return an Integer, 0 for Good and 1 for cancelled.</p> <p>And finally, right before the <code>doInBackground</code> method hits return, see if you need to call a cancel on the thread or not.</p> <pre><code>public class asyncTask extends AsyncTask&lt;Void, Void, Void&gt; { private static synchronized boolean cancel; protected void onPreExecute() { cancel = false; } protected String doInBackground(Void ... params) { rm(p); if(cancel) asyncTask.cancel; else return null; } protected void onCancelled() { // only executed if doInBackground resulted in a cancel == true } protected void onPostExecute(Void param) { /// only executed if doInBackground resulted in a cancel == false } private int rm(String str) { if(cancel) return 1; //do part of task if(cancel) return 1; //another part of task if(cancel) return 1; //another part of task return cancel ? 1 : 0; } } </code></pre>
    singulars
    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.
    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