Friday, 1 April 2022

Updating androidx-navigation from v2.3.5 to v2.4.0 causes map info window to disappear

A native android app I am working on has a ViewPager that slides between 2 fragments- one consisting of a list of locations and when a location is clicked, the ViewPager slides to the second fragment which consists of a map with a marker for that location and an info window with its name as seen in this image.

After updating androidx.navigation:navigation-fragment from v2.3.5 to v2.4.0, the info window occasionally fails to appears above the marker even though Marker.isInfoWindowShown() returns true shown here. This is code pertaining to setting the marker & info window in the map fragment:

private void setMarker(Place place) {
    if (map == null) {
        return;
    }

    map.clear();
    binding.mapContainer.requestFocus();
    selectedPlace = place;
    map.moveCamera(
        CameraUpdateFactory.newLatLngZoom(selectedPlace.getLatLng(), MAP_ZOOM_DEFAULT));

    MapMarkerTag markerTag = new MapMarkerTag();
    markerTag.setName(selectedPlace.getName());

    if (selectedPlace.getDrawable() == R.drawable.ic_parking_icon) {
        markerTag.setPermits(((ParkingLot) selectedPlace).getDisplayPermits());
    } else {
        markerTag.setPermits(getString(R.string.n_a));
    }

    MapInfoWindowAdapter infoWindowAdapter = new MapInfoWindowAdapter(requireActivity());
    map.setInfoWindowAdapter(infoWindowAdapter);
    Marker marker = map.addMarker(new MarkerOptions().position(selectedPlace.getLatLng()));
    Objects.requireNonNull(marker).setTag(markerTag);
    marker.showInfoWindow();
}

Here is the code for the custom info window adapter used in the app:

public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private final Activity activity;

    public MapInfoWindowAdapter(Activity a) {
        this.activity = a;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View view = activity.getLayoutInflater().inflate(R.layout.map_info_window, null);

        TextView name = view.findViewById(R.id.info_window_name);
        TextView permitText = view.findViewById(R.id.info_window_permits_label);
        TextView permits = view.findViewById(R.id.info_window_permits);

        MapMarkerTag markerTag = (MapMarkerTag) marker.getTag();

        name.setText(Objects.requireNonNull(markerTag).getName());
        String permit = markerTag.getPermits();
        if (permit.equals(activity.getString(R.string.n_a))) {
            permitText.setVisibility(View.GONE);
            permits.setVisibility(View.GONE);
        } else {
            permits.setText(markerTag.getPermits());
        }

        return view;
    }

    @Override
    public View getInfoContents(@NonNull Marker marker) {
        return null;
    }
}

Furthermore, when clicking the bottom navigation view to navigate away from the map fragment, the MapInfoWindowAdapter.getInfoWindow is called with markerTag being null resulting in a NullPointerException and the app crashing.

Does anybody know why and how updating the androidx.navigation:navigation-fragment dependency would cause such a bug?

Thank you in advance.



from Updating androidx-navigation from v2.3.5 to v2.4.0 causes map info window to disappear

No comments:

Post a Comment