如何为每个 Android 应用程序/进程使用多个 MapActivities/MapViews [英] How to use multiple MapActivities/MapViews per Android application/process
问题描述
我编写了一个 MapActivity
类,它能够显示一组地点以及单个地点.启动时,应用程序创建此 MapActivity
的一个实例并显示多个位置.如果用户点击某个地点,则会启动一个新的 Activity
,显示所选地点的详细信息.此活动有一个菜单项,允许用户查看地图上的地点 - 这会导致创建 MapActivity
的新实例,但现在只显示这个地点.
I have written one MapActivity
class that is able to display a set of places as well as single places. On startup, the application creates an instance of this MapActivity
and displays multiple places. If the user clicks on a certain place, then a new Activity
is launched that shows the details of the selected place. This activity has a menu item that allows the user to view the place on a map - this causes that a new instance of the MapActivity
is created, except that now only this single place is displayed.
现在的问题是,如果用户导航回第一个 MapActivity
(显示多个位置的那个),图块将不再加载 + 有时会遇到 OutOfMemoryErrors.
The problem now is that if the user navigates back to the first MapActivity
(the one that shows multiple places) the tiles won't be loaded anymore + sometimes OutOfMemoryErrors are encountered.
根据Android JavaDocs,每个进程只能有一个MapActivity
.但是,我不想将我的 MapActivity
定义为 singleInstance/singleTask,因为用户应该始终能够导航回显示多个位置的第一个 MapActivity.
According to the Android JavaDocs, it is only possible to have one MapActivity
per process. However, I don't want to define my MapActivity
as a singleInstance/singleTask, since the user should always be able to navigate back to the first MapActivity that shows multiple places.
我已经看到适用于 Android 的 Google Places 应用(随 Google Map 4.4 一起提供)使用多个 MapActivity
实例.这怎么可能?我怎样才能在我的应用程序中实现它?
I have seen that the Google Places app (which has come with Google Map 4.4) for Android uses multiple MapActivity
instances. How is this possible? And how can I achieve it in my application?
推荐答案
根据Android JavaDocs,它只能拥有一个每个类的 MapActivity
According to the Android JavaDocs, it is only possible to have one MapActivity per class
不是每个班级一个地图视图,而是每个进程.
It's not one map view per class, it's per process.
众所周知,在一个进程中使用多个地图视图时可能会遇到一些问题.如果您没有配置任何特定的东西,通常就是这种情况(您的应用程序在一个进程中运行).您可以使用清单中的 android:process 属性来分配给您的活动:
It's known that you might experience some issues when using multiple mapviews in one process. Usually this is the case (your app running in one process) if you don't configure anything specific. You could though use the android:process attribute in your manifest to assign to your acitivites:
<activity android:name=".activity.directory.MapView1" android:process=":MapView1">
<activity android:name=".activity.directory.MapView2" android:process=":MapView2">
通过这种方式,您可以在单独的进程中运行活动,如果您不在活动之间使用任何共享的静态变量,这种方式会很好地工作.
This way you have the activities running in separate processes, which works well if you don't use any shared static variables across activities.
另见android bug tracker中关于bug的讨论:
Also see the discussion on the bug in the android bug tracker:
http://code.google.com/p/android/问题/详细信息?id=3756
这篇关于如何为每个 Android 应用程序/进程使用多个 MapActivities/MapViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!