旋转手机时如何最好地重新创建标记/折线(方向更改) [英] How best to re-create markers/polylines when phone is rotated (orientation change)

查看:90
本文介绍了旋转手机时如何最好地重新创建标记/折线(方向更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

  • 使用android.support.v4.app.FragmentActivity开发使用Android Google Map v2的本机Android应用.在Android v2.2上运行.
  • Developing a native Android App that uses Android Google Map v2, uses android.support.v4.app.FragmentActivity. Runs on Android v2.2.

目标:

  • 在更改手机方向之前保留在地图上绘制"的标记/折线.

问题:

  1. 我可以将标记/折线保存"为捆绑软件的一部分,并通过使用适当的savedInstance.put..方法将它们保存在onSaveInstanceState中,然后将它们重新存储在onCreate使用适当的savedInstanceState.get..方法.

  1. Can I "save" the markers/polylines as part of the bundle and simply have them re-displayed by saving them in the onSaveInstanceState using the appropriate savedInstance.put.. methods and then "restore" them in onCreate using the appropriate savedInstanceState.get.. methods.

在审查标记getID()的描述时,当我

In reviewing the description for Marker getID() I'm confused when the Google documentation for Marker.getId() method states the following:

从套装中还原地图时,该地图上的标记也将还原.但是,这些标记随后将由不同的Marker对象表示.还原后,可以使用标记的ID来检索Marker对象的新实例.

When a map is restored from a Bundle, markers that were on that map are also restored. However, those markers will then be represented by different Marker objects. A marker's id can be used to retrieve the new instance of a Marker object after such restoration.

Google文档(上面的粗体字)听起来好像标记已被自动还原,而无需采取任何措施.那不是我的经验...也许我误解了所陈述的内容.或者,也许您必须将地图明确保存在捆绑软件中?有人可以弄清楚这是什么意思吗?

The Google documentation (bold text above) makes it sound like the Marker's are just automagically restored without having to take any action. That isn't my experience...Maybe I'm mis-interpreting what is being stated. Or maybe you have to explicitly save map in the Bundle? Can someone clarify what this means?

假设我必须通过适当的savedInstance.put...方法将标记和折线显式保存到束中,否则我应该保存整个标记,还是应该保存标记id并使用标记id检索标记信息到重新显示标记?我找不到可以保存整个标记的put方法.

Assuming I will have to explicitly save the Marker and Polylines to the bundle via the appropriate savedInstance.put... method should I save the entire Marker or should I save the marker id and retrieve the marker info using the marker id to re-display the marker? I couldn't find a put method that would allow me to save the entire Marker.

我注意到 "部分指出以下内容:

I noticed that the MapFragment section of the Google Maps Android API v2 it states the following:

从GoogleMap获得的任何对象都与该视图相关联.重要的是不要在视图的生命周期之内保留对象(例如标记).否则将导致内存泄漏,因为无法释放视图.

Any objects obtained from the GoogleMap is associated with the view. It's important to not hold on to objects (e.g. Marker) beyond the view's life. Otherwise it will cause a memory leak as the view cannot be released.

此语句使我相信,我不应该尝试保存标记本身,而应尝试保存标记ID,然后根据与标记ID关联的标记对象重新生成标记.对于折线也是如此.我的假设正确吗?

This statement leads me to believe that I shouldn't try and save the Marker itself but instead try and save the marker id and then re-generate the marker based on the marker object associated with the marker id. Likewise for the PolyLines. Is my assumption correct?

此外,我应该避免将Marker作为类变量吗?我担心的是,如果Marker是一个类变量,并且将Activity Map片段放在后堆栈上,则可能会导致内存泄漏,因为它会有效地保留在对象上",如上述文档中所述.这是我应该关注的事情吗?

Also, should I avoid having the Marker as a class variable? My concern is that if the Marker is a class variable and the Activity Map fragment is placed on the back stack that this could cause a memory leak because it will effectively be "holding on to the object" as noted in the aforementioned documentation. Is this something I should be concerned about?

致谢.

推荐答案

我可以将标记/折线保存"为捆绑软件的一部分,并通过使用适当的"savedInstance.put .."方法将它们保存在onSaveInstanceState中来简单地重新显示它们,然后使用来将它们恢复"到onCreate中适当的"savedInstanceState.get .."方法.

Can I "save" the markers/polylines as part of the bundle and simply have them re-displayed by saving them in the onSaveInstanceState using the appropriate "savedInstance.put.." methods and then "restore" them in onCreate using the appropriate "savedInstanceState.get.." methods.

否.

Google文档(上面的粗体文本)听起来像标记已被自动还原,而无需执行任何操作.那不是我的经验...也许我误解了所陈述的内容.或者,也许您必须将地图明确保存在捆绑软件中?有人可以弄清楚这是什么意思吗?

The Google documentation (bolded text above) makes it sound like the Marker's are just automagically restored without having to take any action. That isn't my experience...Maybe I'm mis-interpreting what is being stated. Or maybe you have to explicitly save map in the Bundle? Can someone clarify what this means?

您没有误解任何内容.该文档不正确.

You are not mis-interpreting anything. The documentation is incorrect.

我应该保存标记ID并使用标记ID检索标记信息以重新显示标记

should I save the Marker ID and retrieve the marker info using the marker ID to re-display the marker

标记ID在配置更改中不是永久的,它取决于可视对象创建的顺序(第一次调用addMarker会返回ID为"m1"的对象,第二个为"m2"的对象).您不能以任何方式使用此值(从API版本3.1.36开始),恕我直言,它的存在确实没有意义.我实际上总结了与ID相关的问题.至少应该有一个GoogleMap.getMarkerById(String)函数,对于Marker.getId()毫无意义.

Marker ID is not something permanent across configuration changes - it depends on the sequence of visual objects creation (first call to addMarker returns object with ID "m1", second "m2"). You cannot use this value in any way (as of API version 3.1.36) and IMHO there is really no point in its existance. I have actually sumbited an issue related to ID. There should at least be a function GoogleMap.getMarkerById(String) for Marker.getId() to make little sense.

3)另外,我应该避免将Marker作为Class变量吗?我担心的是,如果Marker是一个类变量,并且将Activity Map片段放在后堆栈上,则可能会导致内存泄漏,因为它会有效地保留在对象上",如上述文档中所述.这是我应该关注的事情吗?

3) Also, should I avoid having the Marker as a Class variable? My concern is that if the Marker is a class variable and the Activity Map fragment is placed on the back stack that this could cause a memory leak because it will effectively be "holding on to the object" as noted in the aforementioned documentation. Is this something I should be concerned about?

是的.静态引用Marker或任何其他视觉对象会导致泄漏.

Yes. Keeping a static reference to Marker or any other visual object leads to a leak.

回答您的所有问题和疑虑并不能使它更接近解决方案,所以这是我的主张.

Not that answering all your questions and concerns made it closer to the solution, so here are my propositions.

我假设您有一个数据集,该数据集是从Web服务中获取MarkersPolylines所创建的,并且很可能将其存储在DB中.现在,如果您从ActivityAsyncTask中的数据库中加载它,或者甚至不将其存储在DB中,而只能直接在Activity中获取-那就不好了.

I assume you have a data set that Markers and Polylines are created from and probably store it in DB after being fetched from webservice. Now if you load it from DB in AsyncTask in your Activity or not even store it in DB, but only fetch in Activity directly - that's bad.

请尝试使数据尽可能地可访问,因此仅在进程被终止(或在内存不足时将其删除)之后才需要将其重新加载到内存中.如果您这样做-那很好.当然不是全部:如果您有20000个标记,并且每个标记的图像都显示在信息窗口中,它可以等待...

Try to make your data as accessible as possible, so it only has to be reload into memory after process is killed (or after you drop it when low on memory). If you do that - that's good. Of course not all: if you have 20000 markers and each has its image displayed on info window, it can wait...

现在,您已经拥有在内存中创建标记所需的所有数据,就像第一次创建标记一样.不需要其他代码.

Now that you have all the data needed to create markers in memory, just create them like you would the first time. No additional code required.

我们可以争论这个想法是好是坏,我会改善答案,但这需要有关上下文的更多信息:

We can argue about this being good or bad idea and I will improve the answer, but this requires more info about the context:

  • 多少个标记和折线
  • 您还有什么其他数据
  • 您将模型保存在哪里

当然还有另一种方式:您可以在onSaveInstanceState中发送MarkerOptions.如果您保持最新状态,或者您的Markers不变,并且没有太多更改,这可能对您有用.每次旋转或按HOME按钮,都无法通过IPC发送数千个对象.

There is another way of course: you can send MarkerOptions in onSaveInstanceState. This might work for you if you keep it up-to-date or if your Markers do not change and if there are not so many of them. I can't make sense of sending thousands of objects via IPC on every rotation or pressing HOME button.

这篇关于旋转手机时如何最好地重新创建标记/折线(方向更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆