如何在多个MapActivities上使用单个MapView
问题描述:
我正在开发一个应用程序,该应用程序在三个不同的MapActivities上显示地图。如何在多个MapActivities上使用单个MapView
为了实现这个功能,我在这三个FragmentActivities中重新使用了一个MapFragment,它使用Pete Doyle's port of the Android Compatibility package扩展了MapActivities。
MapFragment中使用的MapView保存在Application Context。
ViewGroup parentViewGroup = (ViewGroup) app.mapViewContainer.getParent();
if(null != parentViewGroup) {
parentViewGroup.removeView(app.mapViewContainer);
}
这一切运作良好,直到那一刻:
为了避免“这一观点已经有母”错误,我打开一个不同的活动时,请从当前父视图我按下手机的后退按钮并转到以前的MapActivity。此时,MapView全部为黑色,因为在更改活动时将其从其父项中移除,并且后退按钮不会触发视图的重新创建...
我知道此帖子: How to use multiple MapActivities/MapViews per Android application/process
由于事实上,我得到了主意,重新使用整个活动的MapView从答案丹尼·雷明顿 - MacroSolve了。
我还没有尝试过使用多个进程,因为我相信我试图实现的解决方案在资源上要轻得多。
任何帮助将不胜感激!
答
固定我自己的问题......
当MapFragment正在恢复我不得不删除片段和来自MapView的母公司所有的意见,然后的MapView添加到片段:
@Override
public void onResume() {
super.onResume();
resumed++;
if (resumed > 0) {
ViewGroup view = (ViewGroup) this.getView();
view.removeAllViews();
ViewGroup parentViewGroup = (ViewGroup) app.mapViewContainer.getParent();
if (parentViewGroup != null) {
parentViewGroup.removeAllViews();
}
view.addView(app.mapViewContainer);
}
}