I have an Activity
with a MapFragment
that I add to the Activity
programmatically using a FragmentTransaction
:
private static final String MAP_FRAGMENT_TAG = "map";
private MapFragment mapFragment = null;
...
protected void onCreate(Bundle savedInstanceState) {
...
mapFragment = (MapFragment) getFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
...
}
Standard way. Then I get the GoogleMap
instance from the mapFragment
and set its settings, set the listeners, do stuff with it. Everything works fine.
Then when the user is done with the map, an AsyncTask
gets triggered to show a ProgressDialog
, perform some operation, put a different fragment into the fragment_wrapper
and dismiss the ProgressDialog
again:
private class GetFlightsTask extends AsyncTask<Double, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// the activity context has been passed to the AsyncTask through its constructor
loadingFlightsSpinner = new ProgressDialog(context);
// setting the dialog up
loadingFlightsSpinner.show();
}
@Override
protected String doInBackground(Double... params) {
// some pretty long remote API call
// (loading a JSON file from http://some.website.com/...)
}
@Override
protected void onPostExecute(String flightsJSON) {
super.onPostExecute(flightsJSON);
// here I do stuff with the JSON and then I swtich the fragments like this
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
FlightsFragment fragment = new FlightsFragment();
fragmentTransaction.replace(R.id.fragment_wrapper, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
loadingFlightsSpinner.dismiss();
}
Everything still works fine. The user does something in the FlightsFragment
and then maybe decides to go back to the map. Presses the back button and the map pops up again. And this is when the map gets laggy. The countries/cities names on it load really slowly, it lags heavily on moving the map... And I have no idea why, I don't do anything on popping the MapFragment
back.
What's interesting is that it gets fixed for example on pressing the home button and then returning to the app again...
What am I doing wrong?
Thank you for any ideas.
ProgressDialog
completely into theAsyncTask
doesn't help either. – Fortieth