Android地图,标记和内存泄漏标记、内存、地图、Android

2023-09-06 06:19:15 作者:犭良)中(王

我读了Android文档 的http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html 我碰到了这样一句话:

I was reading the android docs http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html and I came across this sentence:

在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.

我不完全理解这一点,我不知道它适用于我,但我只是想检查:这仅适用于该片段被销毁,而主视图仍然存在,对不对?我的地图片段是在布局的XML的唯一元素,所以我想,当用户离开时,标记物(和其他一切)被摧毁。我说得对呢,还是相反?

I don't fully understand that, and I'm not sure that it applies to me, but I just wanted to check: this only applies if the fragment is destroyed while the main view still exists, right? My map fragment is the only element in that layout's xml, so I assume that when the user navigates away, the marker objects (and everything else) get destroyed. Am I right, or is it the opposite?

推荐答案

如果你看一个片段的生命周期你可以看到,虽然片段仍存有观点可能被破坏。它也可以重新创建视图之前的片段被破坏。这只是意味着你应该清理和创造的 onCreateView 和 onDestroyView 回调代替。如果您使用的这些回调标记操作,你应该罚款。

If you look at a fragments lifecycle you can see that the view may be destroyed while the fragment still lives. It can also recreate the view before the fragment is destroyed. This just means you should cleanup and create all markers in the onCreateView and onDestroyView callbacks instead. If you use those callbacks for marker manipulation you should be fine.

 
精彩推荐