какой путь является правильным, чтобы предотвратить воссоздание карты Google? - PullRequest
0 голосов
/ 04 января 2019

В моем проекте навигация снизу.

fragmentManager.beginTransaction().add(R.id.container, profile, "profile").detach(profile).commit();
fragmentManager.beginTransaction().add(R.id.container, news, "news").detach(news).commit();
fragmentManager.beginTransaction().add(R.id.container, map, "map").commit();
currentFragment = map;

Я перемещаюсь между фрагментами, используя detach / attach.

fragmentManager.beginTransaction().detach(currentFragment).commit();
fragmentManager.beginTransaction().attach(newFragment).commit();
currentFragment = newFragment; 

Карта Google воссоздается, и onMapReady вызывает каждый раз, когда я прикрепляю фрагмент к карте. В результате карта не сохраняет свои свойства. Как правильно перемещаться между фрагментами или сохранять свойства карты?

UPD: MapFragment

private static final String TAG = "MapFragment";
private static final int ERROR_DIALOG_REQUEST = 9001;
private static final float MOSCOW_ZOOM = 9.8f;
private static final LatLng MOSCOW_LATLNG = new LatLng(55.751498, 37.618767);

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_map, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (isServicesOK()) {
        initMap();
    }
}

private void initMap() {
    SupportMapFragment mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
            .findFragmentById(R.id.map);

    assert mMapFragment != null;
    mMapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    googleMap.setMinZoomPreference(8f);
    LatLngBounds MOSCOW = new LatLngBounds(
            new LatLng(55.40217, 37.23093), new LatLng(56.10699, 37.95694));

    googleMap.setLatLngBoundsForCameraTarget(MOSCOW);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MOSCOW_LATLNG, MOSCOW_ZOOM));
}

public boolean isServicesOK(){
    int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getContext());

    if(available == ConnectionResult.SUCCESS){
        return true;
    }
    else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
        Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(getActivity(), available, ERROR_DIALOG_REQUEST);
        dialog.show();
    }else{
        Toast.makeText(getContext(), "You can't make map requests", Toast.LENGTH_SHORT).show();
    }
    return false;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...