0

I have navigation drawer with fragments. When app starts then the Google map view with markers on it appear on the screen. This is on of the fragment.

Problem 1 When clicking another fragment and returning to the map fragment then it will make a new request to show the map. This happens when using replace().commit();

When using show() and hide() on fragments then it is exactly what I need(until rotating the screen). And it will get kind of complicated when having more than 2 fragments on navigation drawer with all the checking isAdded() what to show and hide and so on. Is there a way to retain the Google map view without making new request with replace().commit(); ?

Problem 2 Same situation, when rotating the screen, Google map fragment makes a new request. And same thing happens when moving between different Activities

Because the Google Maps requests are limited it makes no sense to waste them that way.

I only need one request at the start of the app and then show the same state when navigating around the app.

This is the MapFragment file

public class MapFragment extends android.support.v4.app.Fragment implements OnMapReadyCallback {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);


    mapFragment.getMapAsync(this);


}


@Override
public void onMapReady(GoogleMap map) {

    map.setMyLocationEnabled(true);

    LatLng location= new LatLng(58.6536852, 25.481182);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 7));


     map.addMarker(new MarkerOptions().position(new LatLng(59.408109, 24.326428)).title("marker 1"));
     map.addMarker(new MarkerOptions().position(new LatLng(59.481070, 25.654483)).title("marker 2"));

}

}

Ragnar
  • 255
  • 2
  • 9

1 Answers1

-1

Simple solution to this problem is to call the setRetainInstance(true) after you create you map fragment.

This method does the following functions:

  • Keep the markers after orientation changed.
  • Avoid creating new instances of Map Fragment after screen rotation.
  • Avoid making extra request after each time map is created.

Here is an overview of code snippet:

public class RetainMapFragment extends MapFragment {

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  setRetainInstance(true);
 }
}
AniV
  • 3,997
  • 1
  • 12
  • 17
  • Thanks for your replay, I can't use this method in subFragment. My map fragment is in: Navigation drawer fragment -> one of the sliding tabs. I tried to set it to navigation drawer fragment but not working as expected... – Ragnar Jul 24 '15 at 10:43
  • This post has a similar problem http://stackoverflow.com/questions/27770394/cant-retain-supportmapfragment-inside-fragment It leads to this post: http://stackoverflow.com/questions/12263503/how-can-i-save-the-fragment-when-i-rotate-the-screen where I found a working solution but it is not recommended by adding orientation to android:configChanges in your AndroidManifest.xml. Like this android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" Is there a way to get same result but in a recommended way? – Ragnar Aug 03 '15 at 15:50