旋转时回收Android地图V2 SupportMapFragment [英] Recycle Android Maps V2 SupportMapFragment when rotating
问题描述
我期待改善SupportMapFragment的性能时,该设备被旋转。看来,如果该片段必须重新创建。我不知道这一点,但是当设备旋转的地图图块必须重新加载。它将使意义从性能的角度来对整个mapfragment保留和再利用,而不必重新实例片段。任何洞察到这将是AP preciated。
我声明在XML中SupportMapFragment并使用SetupMapIfNeeded(),如API文档描述。
私人无效setUpMapIfNeeded(){
//做一个空检查确认,我们还没有实例化
// 地图。
如果(MMAP == NULL){
//尝试获取来自SupportMapFragment地图。
MMAP =((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map))的GetMap()。
//检查如果我们成功取得地图。
如果(MMAP!= NULL){
setUpMap();
}
}
}
退房RetainMapActivity类从样品。工程就像一个魅力。这里是:
公共类RetainMapActivity扩展FragmentActivity {
私人GoogleMap的MMAP;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.basic_demo);
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
如果(savedInstanceState == NULL){
//本次活动的第一个化身。
mapFragment.setRetainInstance(真正的);
} 其他 {
//转世活动。得到的地图是在previous相同的地图实例
//活动的生命周期。没有必要重新初始化它。
MMAP = mapFragment.getMap();
}
setUpMapIfNeeded();
}
@覆盖
保护无效onResume(){
super.onResume();
setUpMapIfNeeded();
}
私人无效setUpMapIfNeeded(){
如果(MMAP == NULL){
MMAP =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
.getMap();
如果(MMAP!= NULL){
setUpMap();
}
}
}
私人无效setUpMap(){
mMap.addMarker(新MarkerOptions()位置(新的经纬度(0,0))标题(标记));
}
}
I am looking to improve the performance of the SupportMapFragment when the device is rotated. It seems as if the fragment must be recreated. I am not sure of this, however when the device is rotated the map tiles must be reloaded. It would make sense from a performance perspective to have the entire mapfragment retained and reused without having to re-instantiate the fragment. Any insight into this would be appreciated.
I am declaring the SupportMapFragment in xml and using the SetupMapIfNeeded() as described in the api docs.
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();
}
}
}
Check out the RetainMapActivity class from the samples. Works like a charm. Here is is:
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地图V2 SupportMapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!