我正在使用android兼容类,通过hack在这里找到的片段中使用mapviews:https://github.com/petedoyle/android-support-v4-googlemaps
不幸的是,我发现如果映射片段被从活动中删除,然后被读取,我会得到“你只能在MapActivity中有一个MapView”错误。
我理解了错误背后的原理,并尝试使用fragments onPause方法销毁mapview。不幸的是,我似乎不能完全销毁地图视图,因为我仍然可以得到它。我的代码看起来像这样:
private RelativeLayout layout;
private MapView mp;
public void onResume(){
super.onResume();
Bundle args = getArguments();
if(mp == null)
{
mp = new MapView(getActivity(), this.getString(R.string.map_api_key));
mp.setClickable(true);
}
String request = args.getString("requestId");
layout = (RelativeLayout) getView().findViewById(R.id.mapholder);
layout.addView(mp);
//TextView txt = (TextView) getView().findViewById(R.id.arguments);
//txt.setText(request);
}
public void onPause(){
super.onPause();
layout.removeView(mp);
mp = null;
}有没有人想过我在这里忽略了什么引用?
发布于 2011-12-23 20:32:20
您可能希望删除onPause方法(而不是onDestroy方法)中的地图视图
public void onPause() {
NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
parentView.removeView(this.mMapView);
super.onPause();}通过这种方式,您可以将MapFragment添加到backstack。(FragmentTransaction.addToBackStack防止分片被销毁)
https://stackoverflow.com/questions/7818448
复制相似问题