方向改变时保存 StreetViewFragment 的状态

Save state of StreetViewFragment when orientation changes

我正在尝试将 StreetViewPanorama 的当前位置保存在 StreetViewFragment 中,例如,如果用户最初处于 Portrait 并通过点击箭头更改 StreetViewPanorama 中的位置并探索该区域,然后切换到 Landscape我想设置要恢复的最后位置,以便用户可以继续探索该区域。目前,当我尝试访问 "streetViewPanorama.getLocation()" 时,streetViewPanorama 为 Null,但这不是问题所在。我认为我的救国方式不对,应该有更好的方式。所以我征求你的建议,拜托!

这是我的 StreetViewFragment 的代码:

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

    }
}

在稍微修改之后,我提出了以下解决方案,它保存了街景片段当前位置的状态并恢复了它。效果很好。

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

如果扩展片段class,那么使用setRetainInstance方法呢?它允许您保存必要的变量,而不是在设备旋转时破坏您的片段。您可以从我的文章中了解此方法的用法: Simple trick to use and manage Toolbar with Fragments in Android