Inman Douche
Inman Douche

Reputation: 139

Back button only dismissing Progress Dialog and not the AsyncTask

I am trying to cancel my aynctask when the user hits the back button. I have done my research on which methods to use for this operation and I found it difficult to figure out why are there more than one method. I have found one method and tried it but it only dismisses my ProgressDialog and my aynctassk still executes. So can somebody assist me with this issue of getting my asynctask to dismiss the proper way.

@Override
public void onBackPressed() 
{              
    /** If user Pressed BackButton While Running Asynctask
        this will close the ASynctask.
     */
    if (mTask != null && 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 && 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();

  }  

}

And please let me know If I need to post my doInBackGround method to the question

protected String doInBackground(String... args) {

    try {
        Intent in = getIntent();
        String searchTerm = in.getStringExtra("TAG_SEARCH");
        String query = URLEncoder.encode(searchTerm, "utf-8");
        String URL = "http://example.com";
        JSONParsser jParser = new JSONParsser();
        JSONObject json = jParser.readJSONFeed(URL);
        try {

            JSONArray questions = json.getJSONObject("all").getJSONArray("questions");

            for(int i = 0; i < 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);



                       HashMap<String, String> map = new HashMap<String, String>();

                       map.put(TAG_QUESTION_SUBJECT, Subject);
                       map.put(TAG_QUESTION_CONTENT, Content);
                       map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);

                       questionList.add(map);

            }


            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

                    return null;

}

Upvotes: 0

Views: 605

Answers (1)

Quanturium
Quanturium

Reputation: 5696

I think the problem is in the doInBackground method of your AsyncTask. When you cancel your AsyncTask with mTask.cancel(true); it does not cancel the task, it just set a flag. You need to check in the DoInBackground method if the Task has been canceled (flagged as cancel) and return if it has.

Example :

protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

Source : http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 3

Related Questions