2

我正在开发一个在三个不同的 MapActivity 上显示地图的应用程序。

为了实现这一点,我在这三个 FragmentActivities 中重新使用了一个 MapFragment,它使用Pete DoyleAndroid 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

事实上,我从Danny Remington - MacroSolve给出的答案中得到了在活动中重用 MapView 的想法。

我没有尝试使用多个进程,因为我相信我尝试实施的解决方案在资源上要轻得多。

任何帮助将非常感激!

4

1 回答 1

1

解决了我自己的问题...

当 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);
    }
}
于 2012-01-05T17:10:09.227 回答