androidkotlinandroid-mapviewnavcontroller

MapView crash after first load with onBackPressed(), navigateUp()


im trying to work with a MapView,

I have almost automated navigation with NavController, but when I go back (navigateUp()) and have to load the map I get this error

com.google.maps.api.android.lib6.common.apiexception.c: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occurred for the map view.  Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
                                                                                                        at com.google.maps.api.android.lib6.common.m.h(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):8)
                                                                                                        at com.google.maps.api.android.lib6.gmm6.api.c.l(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):28)
                                                                                                        at com.google.maps.api.android.lib6.impl.ae.a(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):10)
                                                                                                        at com.google.maps.api.android.lib6.gmm6.api.c.z(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):31)
                                                                                                        at com.google.maps.api.android.lib6.impl.bk.q(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):25)
                                                                                                        at com.google.android.gms.maps.internal.i.bb(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):1959)
                                                                                                        at m.fi.onTransact(:com.google.android.gms.dynamite_mapsdynamite@233918037@23.39.18 (150400-0):21)

I know it is easier to use the SupportMapFragment, since everything is automated, but I would like to know why I am having this error.

I have these methods implemented:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mapView = binding.root.findViewById(R.id.gm_resource)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync(this)
    }
override fun onMapReady(p0: GoogleMap) {
        map = p0
        map.uiSettings.isMyLocationButtonEnabled = false
        map.mapType = GoogleMap.MAP_TYPE_SATELLITE
        map.setOnMapClickListener { requireContext().hideKeyboard(binding.root) }
        createClusterManager(map)
        loadResources()
        mapFocus(false)
        moveToolbar()
    }

I also try to control the life of the MapView

override fun onResume() {
        mapView.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        mapView.onDestroy()
        super.onDestroy()
    }

    override fun onLowMemory() {
        mapView.onLowMemory()
        super.onLowMemory()
    }

I don't know if this is the correct way, if someone knows how to make a mapView work correctly, controlling its life and its loads I would be eternally grateful. Thank you


Solution

  • Try to place a SupportMapFragment It's more intuitive and easier to work with so it won't give you these troubles