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);
}
}