문제

When I go back to the previous activity by pressing the back button the progress dialog is appearing and not disappearing. When I minimize they the app the progress dialog disappears.

Here is the code for the async class

public class BackGroundTask extends AsyncTask<String, String, JSONObject> {
    List<NameValuePair> postparams = new ArrayList<NameValuePair>();
    private ProgressDialog pd;
    String url = null;
    String method = null;
    Context context;


    public BackGroundTask(String url, String method,
            List<NameValuePair> params, Context context) {
        this.url = url;
        postparams = params;
        this.method = method;
        this.context = context;

        //pd = new ProgressDialog(context);
        //pd.setTitle("Processing...");
        //pd.setMessage("Please wait.");
        //pd.setCancelable(false);
        //pd.setIndeterminate(true);

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd = ProgressDialog.show(context, "Processing...", "Please wait.", true, false);

    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pd.dismiss();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // TODO Auto-generated method stub
        // Making HTTP request
        try {
            // Making HTTP request
            // check for request method

            if (method.equals("POST")) {
                // request method is POST
                // defaultHttpClient
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(postparams));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                HttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(postparams,
                        "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        System.out.println(json);

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        //pd.dismiss();

        // return JSON String
        return jObj;

    }


}
도움이 되었습니까?

해결책

You aren't dismissing it when you finish the Activity and the task must not be done. Override finish() and dismiss it if needed

@Override
public void finish()
{
     if (pd.isShowing()
     {
          pd.dismiss();
     }
     super.finish();
}

You could also Override onBackPressed() and put this code there but since pressing the back button calls finish() its probably safer just to do it there.

Also, you are comparing Strings correctly in one place

if (method.equals("POST"))  // correct

but not others

else if (method == "GET")  // incorrect
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top