Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I believe I've solved this.</p> <p>In my Activity class I have a variable (boolean) <code>failed</code>. Also, I have a private <code>Downloader</code> class within the activity which extends <code>ASyncTask</code>. This way, the <code>Downloader</code> class has access to the <code>failed</code> boolean. When the Activity launches, it starts the <code>Downloader</code> task and a progress dialog pops up. When the task finishes, it closes the dialog and then goes on processing the downloaded content.</p> <p>However, when the user cancels the progress dialog, <code>failed</code> is set to true, and the user is sent back to the previous activity by a call to <code>finished</code>. In the meantime, <code>Downloader</code> is still busy downloading. Because the results are now unneccessary, we want it to stop using resources asap. In order to accomplish this, I have broken up the <code>doInBackground</code> method in as much steps as possible. After each step I check if <code>failed</code> is still <code>false</code>, when it is set to <code>true</code>, it simply doesn't go to the next step. See it in action below. Furthemore, the <code>BufferedReader reader</code> is public, and in the <code>onCancelled</code> method I execute <code>reader.close()</code>. This will throw all sorts of exceptions, but these are properly caught. </p> <pre><code> public void DoInBackground(.........) { try { URL url = new URL(uri); URLConnection conn = url.openConnection(); if (!failed) { isr = new InputStreamReader(conn.getInputStream()); if (!failed) { reader = new BufferedReader(isr); publishProgress(1); if (!failed) { TagNode node = cleaner.clean(reader); publishProgress(2); return node; } } } } catch (Exception e) { failed = true; Log.v("error",""+e); } } @Override protected void onCancelled() { failed = true; if (reader != null) try { reader.close(); } catch (IOException e) { failed = true; } if (isr != null) try { isr.close(); } catch (IOException e) { } } </code></pre> <p>I know that I could have broken up the downloading process in even tinier bits, but I am downloading very small files, so it's not that important.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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