0

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

faizy
  • 504
  • 5
  • 17
  • possible duplication of [link](https://stackoverflow.com/questions/54581071/fragments-destroyed-recreated-with-jetpacks-android-navigation-components) – Ahmad Salman Jun 08 '21 at 08:42

1 Answers1

0

I have solved the issue, using this link

Created a BaseFragment file

open class BaseFragment : Fragment() {
    var hasInitializedRootView = false
    private var rootView: View? = null

    fun getPersistentView(
        binding: ViewBinding
    ): View? {
        if (rootView == null) {
            rootView = binding.root
        } else {
           
            (rootView?.getParent() as? ViewGroup)?.removeView(rootView)
        }

        return rootView
    }
}

Then on my fragment

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
    
        binding = FragmentDestinationViewBinding.inflate(LayoutInflater.from(context))
    
        return getPersistentView(
            binding
        )
    }


        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    if (!hasInitializedRootView) {
                hasInitializedRootView = true
                mMapView = binding.MapView
    
                mMapView.onCreate(savedInstanceState)
                mMapView.onResume()
                mMapView.getMapAsync(this)
                mLocationRequest = LocationRequest()
            }
*****************
    }
faizy
  • 504
  • 5
  • 17