Stack two
Stack two

Reputation: 541

Android ProgressDialog stops spinning before the task is completed

I am using progress dialog in my Asynctask class on onpreExecte() method, and dismissing the dialog in onPostExecute while google makes the routes of the map. My problem is the wheel in dialog is stopping after 2-3 seconds but my background process is still working.

    private class ParserTask extends
            AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

   @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(MapaViagem.this);
        // setup your dialog here
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle("Traçando Rotas");
        dialog.setMessage("Aguarde...");
        dialog.setCancelable(false);
        dialog.show();

    }

        @Override
        protected List<List<HashMap<String, String>>> doInBackground(
                String... jsonData) {


            JSONObject jObject;
            List<List<HashMap<String, String>>> routes = null;

            try {
                jObject = new JSONObject(jsonData[0]);
                PathJSONParser parser = new PathJSONParser();
                routes = parser.parse(jObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return routes;
        }

        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
            ArrayList<LatLng> points = null;
            PolylineOptions polyLineOptions = null;

            // traversing through routes
            for (int i = 0; i < routes.size(); i++) {
                points = new ArrayList<LatLng>();
                polyLineOptions = new PolylineOptions();
                List<HashMap<String, String>> path = routes.get(i);

                for (int j = 0; j < path.size(); j++) {
                    HashMap<String, String> point = path.get(j);

                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng position = new LatLng(lat, lng);

                    points.add(position);
                }

                polyLineOptions.addAll(points);
                polyLineOptions.width(4);
                polyLineOptions.color(Color.BLUE);
            }

            googleMap.addPolyline(polyLineOptions);
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

Upvotes: 0

Views: 490

Answers (1)

RedDeath
RedDeath

Reputation: 322

You must put all the "procesing" code inside the doInBackground, that means all the code you have inside the onPostExcecute. Only the things that modify the UI should be in the onPostExcecute (like for example show a message to the user, change text of a TextView, etc.), else you will get your UI stucked. I had the same problem and resolved it this way.

Hope it helps

Upvotes: 1

Related Questions