Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle isCancelled in AsyncTask
    primarykey
    data
    text
    <p>I am trying to add this code to my <code>doInBackGround</code> so I can catch a flag that is being set when the user presses the back button </p> <pre><code>protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i &lt; count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } </code></pre> <p>This my <code>doInBackGround</code> and the code I use to set the flag for the asynctask to be canceled </p> <pre><code>@Override public void onBackPressed() { /** If user Pressed BackButton While Running Asynctask this will close the ASynctask. */ if (mTask != null &amp;&amp; mTask.getStatus() != AsyncTask.Status.FINISHED) { mTask.cancel(true); } super.onBackPressed(); finish(); } @Override protected void onDestroy() { // TODO Auto-generated method stub /** If Activity is Destroyed While Running Asynctask this will close the ASynctask. */ if (mTask != null &amp;&amp; mTask.getStatus() != AsyncTask.Status.FINISHED) { mTask.cancel(true); } super.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub if (pDialog != null) { if(pDialog.isShowing()) { pDialog.dismiss(); } super.onPause(); } } class LoadAllData extends AsyncTask&lt;String, String, String&gt; { protected String doInBackground(String... args) { try { Intent in = getIntent(); String searchTerm = in.getStringExtra("TAG_SEARCH"); String query = URLEncoder.encode(searchTerm, "utf-8"); String URL = "example.com"; JSONParsser jParser = new JSONParsser(); JSONObject json = jParser.readJSONFeed(URL); try { JSONArray questions = json.getJSONObject("all").getJSONArray("questions"); for(int i = 0; i &lt; questions.length(); i++) { JSONObject question = questions.getJSONObject(i); String Subject = question.getString(TAG_QUESTION_SUBJECT); String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER); String Content = question.getString(TAG_QUESTION_CONTENT); </code></pre> <p>The problem comes when I try to translate <code>isCancelled</code> into my asynctask. I get an error under <code>Downloader</code> that says "<code>Downloader</code> cannot be resolved" I also get one under <code>publishProgress</code> that says "The method <code>publishProgress</code>(String...) in the type AsyncTask is not applicable for the argument (int)" I'm jus tasking is somebody can help put the <code>isCancelled</code> into my AsyncTask. I have also done my research and seen that there are more than one way to use <code>isCancelled</code> in your code. I have realized that dealing with <code>isCancelled</code> can really be a hassle for somebody new to it.</p>
    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.
 

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