gpt4 book ai didi

android - 如何跨多个 MapActivity 使用单个 MapView

转载 作者:行者123 更新时间:2023-11-29 02:08:22 33 4
gpt4 key购买 nike

我正在开发一个在三个不同的 MapActivities 上显示 map 的应用。

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

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

为了避免“这个 View 已经有一个父 View ”错误,我在打开不同的 Activity 时从当前父 View 中删除了 View :

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

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

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

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

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

如有任何帮助,我们将不胜感激!

最佳答案

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

恢复 MapFragment 时,我只需要从 fragment 和 mapview 的父 View 中删除所有 View ,然后将 mapview 添加到 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);
}
}

关于android - 如何跨多个 MapActivity 使用单个 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732555/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com