gpt4 book ai didi

android - 旋转时回收Android Maps V2 SupportMapFragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:49 24 4
gpt4 key购买 nike

我希望在设备旋转时提高 SupportMapFragment 的性能。似乎必须重新创建 fragment 。我不确定这一点,但是当设备旋转时,必须重新加载 map 图 block 。从性能的角度来看,保留和重用整个 mapfragment 而不必重新实例化 fragment 是有意义的。对此有任何见解将不胜感激。

我在 xml 中声明 SupportMapFragment 并使用 SetupMapIfNeeded(),如 api 文档中所述。

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

最佳答案

检查示例中的 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"));
}

关于android - 旋转时回收Android Maps V2 SupportMapFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com