0

I am trying to save the current position of the StreetViewPanorama in the StreetViewFragment so for example if the user is originally in Portrait and changes the location in the StreetViewPanorama by tapping on the arrows and explore the area and then switches to Landscape I want to set the last location to be restored so the user can continue exploring the area. Currently, the streetViewPanorama is Null when I try to access "streetViewPanorama.getLocation()" but this is not the problem. I think that my way of saving the state is not right and there should be a better way. So I am asking for your suggestions, please!

This is my StreetViewFragment's code:

public class StreetViewFragment extends ViperFragment implements OnStreetViewPanoramaReadyCallback,
    PropertyDetailsStreetView {
private static final String KEY_PROPERTY_ID = "property_id_street_view";
private static final String KEY_PROPERTY_LAT = "property_lat";
private static final String KEY_PROPERTY_LON = "property_lon";
private Bundle savedInstances;

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        streetViewPanoramaFragment = (StreetViewPanoramaFragment)
                getActivity().getFragmentManager().findFragmentById(R.id.streetviewpanorama);
        streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
        savedInstances = savedInstanceState;
    }
 @Override
public void onSaveInstanceState(Bundle outState) {
    outState.putDouble(KEY_PROPERTY_LAT, streetViewPanorama.getLocation().position.latitude);
    outState.putDouble(KEY_PROPERTY_LON, streetViewPanorama.getLocation().position.longitude);
    super.onSaveInstanceState(outState);
}

@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
    this.streetViewPanorama = streetViewPanorama;
    presenter.onStreetViewPanoramaReady(getArguments().getLong(KEY_PROPERTY_ID));

    if (savedInstances != null) {
        this.streetViewPanorama.setPosition(
                new LatLng(savedInstances.getLong(KEY_PROPERTY_LAT),
                        savedInstances.getLong(KEY_PROPERTY_LON)));

    }
}
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126

2 Answers2

0

After giving this a bit more tinkering I came with the following solution wich saves the state of the current position of the street view fragment and restores it. Works good.

public class StreetViewFragment extends ViperFragment implements OnStreetViewPanoramaReadyCallback,
    PropertyDetailsStreetView {
private static final String KEY_PROPERTY_ID = "property_id_street_view";
private static final String KEY_PROPERTY_LAT = "property_lat";
private static final String KEY_PROPERTY_LON = "property_lon";
private StreetViewPanoramaFragment streetViewPanoramaFragment;
private StreetViewPanorama streetViewPanorama;
private Bundle localBundle;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    streetViewPanoramaFragment = (StreetViewPanoramaFragment)
            getActivity().getFragmentManager().findFragmentById(R.id.streetviewpanorama);
    streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
    this.localBundle = savedInstanceState;

}

@Override
public void onSaveInstanceState(Bundle outState) {
    if (streetViewPanoramaFragment != null) {
        outState.putDouble(KEY_PROPERTY_LAT,
                streetViewPanorama.getLocation().position.latitude);
        outState.putDouble(KEY_PROPERTY_LON,
                streetViewPanorama.getLocation().position.longitude);
    }
    super.onSaveInstanceState(outState);
}

@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
    this.streetViewPanorama = streetViewPanorama;
    presenter.onStreetViewPanoramaReady(getArguments().getLong(KEY_PROPERTY_ID));

    if (localBundle != null) {
        this.streetViewPanorama.setPosition(
                new LatLng(localBundle.getDouble(KEY_PROPERTY_LAT),
                        localBundle.getDouble(KEY_PROPERTY_LON)));
    }
}
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
0

If it extends Fragment class, then what about using setRetainInstance method? It allows you to save necessary variable(s) not destoying your fragment when device rotates. You can read about usage of this method from my article: Simple trick to use and manage Toolbar with Fragments in Android

Turkhan Badalov
  • 845
  • 1
  • 11
  • 17