【问题标题】:How to use a single MapView across several MapActivities如何在多个 MapActivity 中使用单个 MapView
【发布时间】:2012-01-04 19:11:32
【问题描述】:

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

为了实现这一点,我在这三个 FragmentActivities 中重复使用 MapFragment,它们使用 Pete Doyle's port of the Android Compatibility package 扩展 MapActivities。

此 MapFragment 中使用的 MapView 保存在 Application Context

为了避免“这个视图已经有一个父级”的错误,我在打开一个不同的activity时从当前父级中移除了这个视图:

ViewGroup parentViewGroup = (ViewGroup) app.mapViewContainer.getParent();
if( null != parentViewGroup ) {
  parentViewGroup.removeView(app.mapViewContainer);
}

在我按下手机的后退按钮并进入上一个 MapActivity 之前,一切正常。这时候MapView全黑了,因为我在更改activity时把它从它的parent中移除了,并且返回按钮并没有触发视图的重新创建...

我知道这篇文章: How to use multiple MapActivities/MapViews per Android application/process

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

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

任何帮助将不胜感激!

【问题讨论】:

    标签: android android-mapview android-fragments mapactivity


    【解决方案1】:

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

    当 MapFragment 恢复时,我只需要从片段和地图视图的父级中删除所有视图,然后将地图视图添加到片段:

    @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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多