*Platform:Android 7.0*
*Mapbox SDK version:5.2.1*
Mapview closed
App generates ANR and crashes after some seconds
If I grey out 'mapView.onDestroy()' the problem doesn't exists anymore. I'm testing on physical device (moto g5). I get no error exception in Android studio. App stops working after mapView.onDestroy(). This is my code:
`
public class PremiumRoutesMapFragment extends Fragment {
private MapView mapView;
private MapboxMap map;
private Icon iconnormal;
private Icon iconpremium;
private RouteParameter rp = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Mapbox.getInstance(getActivity(), getString(R.string.mapbox_access_token));
rp = PremiumRoutes.rp;
View root = inflater.inflate(R.layout.premium_routes_map_fragment, container, false);
mapView = (MapView) root.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Add a MapboxMap
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final MapboxMap mapboxMap) {
map = mapboxMap;
LatLng startLocation = new LatLng();
startLocation.setLatitude(51.01368);
startLocation.setLongitude(4.20113);
// Customize map with markers, polylines, etc.
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 7));
mapboxMap.getUiSettings().setTiltGesturesEnabled(false);
mapboxMap.getUiSettings().setRotateGesturesEnabled(false);
mapboxMap.getUiSettings().setCompassEnabled(false);
mapboxMap.setMaxZoomPreference(17);
mapboxMap.setMinZoomPreference(1);
mapboxMap.getMarkerViewManager().setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) {
if (marker.getTitle().equals("premiumroute") && Helper.hasAbo(getApplicationContext()) == false) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NoSubscription.class);
Bundle b = new Bundle();
b.putInt("TYPE", 1);
intent.putExtras(b);
startActivity(intent);
}
else {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), PremiumRouteDetails.class);
Bundle b = new Bundle();
b.putString("ROUTE", marker.getSnippet());
intent.putExtras(b);
startActivity(intent);
}
return true;
}
});
}
});
return root;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
`
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
needs to be replaced with:
@Override
public void onDestroyView() {
super.onDestroyView();
mapView.onDestroy();
}
This is noted in the javadoc of MapView#onDestroy()
Most helpful comment
needs to be replaced with:
This is noted in the javadoc of
MapView#onDestroy()