Your problem is basically user experience related and not to your code.
There are two solutions you can use that will tell the user the app is doing something and not just frozen or have crashed :
Solution 1
You can use a loading view that is displayed instead of your map while your loading information
<FrameLayout
<!-- same layout information as your current MapView -->
...
>
<MapView
...
/>
<View
<!-- This can be a ProgressBar or a custom loading View with animation -->
/>
</FrameLayout>
You can then make View Visible
when data is loading and then display map once finished.. this'll help avoid the black screen
Update
Following this answer, you can find to extend a Fragment
and add a MapView
Setting up the layout for showing the map. location_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/loadingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Now we code the java class for showing the Map. MapViewFragment :
public class MapViewFragment extends Fragment {
MapView mMapView;
View mLoadingView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mLoadingView = rootView.findViewById(R.id.loadingView); // you can handle your view as you wish
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
This way you can hide/show loadingView
whenever you've done fetching data.
Solution 2
You can display only a map before loading the data then display a ProgressBar
on top of it while fetching data. Once data is loaded you can populate your map.