1

I have an Async task in my main activity which runs when I press a button. Now for some reason if the request takes too long(due to the server being unavailable) I am unable to even close the activity using the back button. Does anyone know why this is happening? From this Android AsyncTask blocks back key dispatch Event I understand that the back button should automatically kill my activity and finish the async task. But that does not seem to be happening. The only way I can come out of the activity is by pressing the home button on my android phone which of course simply saves the state so once I open up the activity again the async task still continues to execute.

class mytask extends AsyncTask<Void,Void,String>
        {
            HttpResponse response;



            @Override
            protected void onPreExecute()
            {
                // TODO Auto-generated method stub
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(Void... arg0) 
            {
                // TODO Auto-generated method stub
                Log.d("In","IN");


                    HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("someurl");

                try 
                {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("name", "somename"));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);
               p=inputStreamToString(response.getEntity().getContent()).toString();        
                     return p;


                } 
                catch (ClientProtocolException e) 
                {
                    // TODO Auto-generated catch block
                } 
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                }

                return null;
            }
            protected void onPostExecute(String result) 
            {
                // TODO Auto-generated method stub
                super.onPostExecute(result);



            }



        }
Community
  • 1
  • 1
Developer Android
  • 577
  • 3
  • 5
  • 21
  • 1
    Show us some code please – shkschneider Oct 22 '12 at 13:52
  • I have posted some code but I dont think that it is the cause for my activity not closing when I press the back button. Even if I dont override the back button the task should ideally kill itself according tot he linked question – Developer Android Oct 22 '12 at 14:01
  • i don't understand the part where the activity does not close. post the activity back pressed handling part – njzk2 Oct 22 '12 at 14:21
  • I dont trap the back button at all according to http://stackoverflow.com/questions/6023350/android-asynctask-blocks-back-key-dispatch-event – Developer Android Oct 22 '12 at 14:56

2 Answers2

0

do the following:

your asynctask class object for example:

       mytask.cancel(true)

on click of back button

G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
0

You have override the onCancelled() method of your async task and call yourAsync.cancel(true) on back press. One more thing if you are using some for loop in doInBackground() than you should put a check for isCancelled().

sachy
  • 739
  • 7
  • 13