I am using MapView
to list some destinations. but when iam going to the destination details screen and came back to the MapView fragment, the map being regenerating(it look like when first loading the fragment, and fetching the destination, placing the markers). why is this happening?.
i tried to save state with onSaveInstanceState
but this isn't calling when i go to the destination details screen.
private lateinit var mGoogleMap: GoogleMap
private lateinit var mMapView: MapView
*********
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mMapView = binding.MapView
mMapView.onCreate(savedInstanceState)
mMapView.onResume()
mMapView.getMapAsync(this)
mLocationRequest = LocationRequest()
val locationManager =
activity?.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps()
}
}
override fun onMapReady(googleMap: GoogleMap?) {
Log.e("onMapReady", "onMapReady")
if (googleMap != null) {
mGoogleMap = googleMap
if (checkPermission()) {
startLocationUpdates()
}
googleMap.setOnMarkerClickListener(this)
}
}
the result i want to be is, when i came back to the fragment from details page, the map view screen should be look like before. How could i possible?
I tried to save the instance. but it never calls while navigating to the details screen
override fun onSaveInstanceState(savedInstanceState: Bundle) {
// saving the last zoom, coordinates
val lat = mGoogleMap.cameraPosition.target.latitude
val lon = mGoogleMap.cameraPosition.target.longitude
val zoom = mGoogleMap.cameraPosition.zoom
savedInstanceState.putDouble("map_lat", lat)
savedInstanceState.putDouble("map_lon", lon)
savedInstanceState.putFloat("map_zoom", zoom)
Log.e("onSaveInstanceState", "onSaveInstanceState")
super.onSaveInstanceState(savedInstanceState)
}
note: i am using navigation component and viewmodel