如何在多个 MapActivity 中使用单个 MapView

发布于 2024-12-25 01:16:42 字数 1123 浏览 1 评论 0原文

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

为了实现这一目标,我在这三个 FragmentActivities 中重复使用 MapFragment,这些 FragmentActivities 使用 Pete Doyle 移植的 Android 兼容性包

此 MapFragment 中使用的 MapView 保存在 应用程序上下文 中。

为了避免“此视图已经有父级”错误,我在打开不同的活动时从当前父级中删除了视图:

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

一切正常,直到我按下手机的后退按钮并到达上一个 MapActivity。此时,MapView 是全黑的,因为我在更改活动时将其从其父级中删除,并且后退按钮不会触发视图的重新创建...

我知道这篇文章: 如何在每个 Android 应用程序/进程中使用多个 MapActivities/MapView

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

我没有尝试使用多个进程,因为我相信我尝试实现的解决方案占用的资源要少得多。

任何帮助将不胜感激!

I'm developing an app that shows a map on three different MapActivities.

To accomplish this I re-use a MapFragment across this three FragmentActivities which extend MapActivities using the Pete Doyle's port of the Android Compatibility package.

The MapView used in this MapFragment is kept at the Application Context.

In order to avoid the "this view already has a parent" error, I remove the view from the current parent when opening a different activity:

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

It all works well until the moment when i press the phone's back button and get to a previous MapActivity. At this time, the MapView is all black, since I removed it from its parent when changing activities, and the back button doesn't trigger a re-creation of the view...

I'm aware of this post:
How to use multiple MapActivities/MapViews per Android application/process

As a matter of fact, I got the idea to re-use the MapView across activities from the answer Danny Remington - MacroSolve gave.

I haven't tried to use multiple processes since I believe the solution I'm trying to implement is much lighter on resources.

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-01 01:16:42

修复了我自己的问题...

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

Fixed my own problem...

When the MapFragment is being resumed I just had to remove all views from the fragment and from the mapview's parent, and then add the mapview to the fragment:

@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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文