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)));
}
}