7

デバイスを回転させたときの SupportMapFragment のパフォーマンスを改善したいと考えています。フラグメントを再作成する必要があるようです。これについてはよくわかりませんが、デバイスを回転させると、マップ タイルをリロードする必要があります。フラグメントを再インスタンス化することなく、マップフラグメント全体を保持して再利用することは、パフォーマンスの観点から理にかなっています。これについての洞察をいただければ幸いです。

xml で SupportMapFragment を宣言し、API ドキュメントで説明されているように SetupMapIfNeeded() を使用しています。

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}
4

1 に答える 1

11

サンプルから RetainMapActivity クラスを確認してください。魅力のように機能します。ここにあります:

public class RetainMapActivity extends FragmentActivity {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    if (savedInstanceState == null) {
        // First incarnation of this activity.
        mapFragment.setRetainInstance(true);
    } else {
        // Reincarnated activity. The obtained map is the same map instance in the previous
        // activity life cycle. There is no need to reinitialize it.
        mMap = mapFragment.getMap();
    }
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

}

于 2013-02-16T06:58:29.723 に答える