保存在Android的MapFragment(地图V2)国家 [英] Saving MapFragment (Maps v2) State in Android
问题描述
我用新的GoogleMaps API(V2)工作,有一些问题的片段。我有两个片段在我的应用程序(地图列表),用户可以在它们之间进行切换。正常工作到目前为止这一code:
如果(itemPosition == 0){
getSupportFragmentManager()调用BeginTransaction()
.replace(R.id.fragmentpos,myMapFragment).commit();
}否则如果(itemPosition == 1){
getSupportFragmentManager()调用BeginTransaction()
.replace(R.id.fragmentpos,myListFragment).commit();
} ......
当我与地图工作,切换到列表中的片段,然后回到地图片段,在地图的最后一个位置不被保存。这不是我想要的,我想,该图是在保存状态,因为我左呢..
我怎样才能做到这一点?我试图保存在摄像机位置
公共无效的onSaveInstanceState(捆绑outState){
outState.putDouble(LAT,mMap.getCameraPosition()target.latitude);
outState.putDouble(LON,mMap.getCameraPosition()target.longitude);
outState.putFloat(熊,mMap.getCameraPosition()轴承);
...
}
和它onCreateView()恢复,但捆绑总是空。如果我更换了片段的onSaveInstanceState不叫。
所以,我怎么能保存地图的状态?
我怎样才能做到这一点?
块引用>如果您
替换()
片段,老段的意见被破坏,这需要你的图形页面
和,presumably,在CameraPosition
。
的onSaveInstanceState()
主要是为配置更改,如屏幕旋转。MapFragment
和SupportMapFragment
已经保留了CameraPosition
(其中,顺便说一句是Parcelable
,这样可以节省在整个对象中的捆绑
而不是零碎的)。您可以考虑使用
显示()
和隐藏()
而不是更换( )
,所以片段其观点坚持围绕。I'm working with the new GoogleMaps API (v2) and have some problems with the fragment. I have two fragments in my app (Map, List) and the user can switch between them. Works fine so far with this code:
if (itemPosition == 0) { getSupportFragmentManager().beginTransaction() .replace(R.id.fragmentpos, myMapFragment).commit(); } else if (itemPosition == 1) { getSupportFragmentManager().beginTransaction() .replace(R.id.fragmentpos, myListFragment).commit(); }......
When I work with the map, switch to the list fragment and then back to the map fragment, the last position of the map is not saved. This is not what i want, i want that the map is in the save state as i "left" it.. How can I accomplish this? I tried to save the camera position in
public void onSaveInstanceState(Bundle outState){ outState.putDouble("LAT", mMap.getCameraPosition().target.latitude); outState.putDouble("LON", mMap.getCameraPosition().target.longitude); outState.putFloat("BEAR", mMap.getCameraPosition().bearing); ...
}
and restore it in onCreateView(), but the Bundle is always null. The onSaveInstanceState is not called if I replace the fragments.
So how can I save the state of the map?
解决方案How can I accomplish this?
If you
replace()
a fragment, the old fragment's views are destroyed, which takes out yourMapView
and, presumably, theCameraPosition
.
onSaveInstanceState()
is mostly for configuration changes, such as screen rotations.MapFragment
andSupportMapFragment
already retain theCameraPosition
(which, BTW, isParcelable
, so you can save the whole object in theBundle
rather than piecemeal).You could consider using
show()
andhide()
instead ofreplace()
, so the fragment and its views sticks around.这篇关于保存在Android的MapFragment(地图V2)国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!