如何在多个 MapActivity 中使用单个 MapView
我正在开发一个应用程序,在三个不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修复了我自己的问题...
当 MapFragment 恢复时,我只需从片段和地图视图的父级中删除所有视图,然后将地图视图添加到片段中:
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: