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