OnMapReadyCallback not called in Fragment

Hi,

I’ve got a mapView set up in a Fragment and i don’t get onMapReadyCallback even tho the map is visible. I tried using a mapFragment and i get the same behaviour.

<androidx.fragment.app.FragmentContainerView
android:id="@+id/mapFragment"
android:name=“com.tomtom.online.sdk.map.MapFragment”
tomtom:mapStyleSource=“STYLE_MERGER”
android:layout_width=“match_parent”
android:layout_height=“200dp”
android:layout_marginBottom=“16dp”
android:layout_marginTop=“20dp”
android:layout_marginStart=“20dp”
android:layout_marginEnd=“20dp”
android:background="#A3A1A1"
app:layout_constraintTop_toBottomOf="@id/infotitle"
app:layout_constraintBottom_toTopOf="@+id/divider1"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
tomtom:mapsApiKey=“api key”/>

FragmentManager fm = getChildFragmentManager();
mapFragment = (MapFragment) fm.findFragmentById(R.id.mapFragment);
if (mapFragment != null){
fm.beginTransaction().replace(R.id.mapView, mapFragment).commitNow();
mapFragment.getAsyncMap(tomtomMap → );
}

/// Map View
<com.tomtom.online.sdk.map.MapView
android:id="@+id/mapView"
android:layout_width=“match_parent”
android:layout_height=“200dp”
android:layout_marginBottom=“16dp”
android:layout_marginTop=“20dp”
android:layout_marginStart=“20dp”
android:layout_marginEnd=“20dp”
android:background="#A3A1A1"
app:layout_constraintTop_toBottomOf="@id/infotitle"
app:layout_constraintBottom_toTopOf="@+id/divider1"
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
tomtom:mapsApiKey=“api key”/>
MapView mapView = new MapView(requireContext).findViewbyId(R.id.mapView);
mapView.addOnMapReadyCallback…

Update: I don’t get a callback when map is ready because the view that contains the map is on Visibility: GONE when i open the fragment. Basically the fragment’s main view doesn’t contain the map but a view that you open when you click on an item. Using Visibility: INVISIBLE fixes that problem. If i set that view on Visibility GONE on Back Press Button again the map is destroyed. There is a bug that doesn’t allow the view containing that map to be on Visibility Gone. So just use Visibility: Invisible when you want to hide that view.

1 Like