android maps, markers and memory leaks
Asked Answered
D

1

3

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

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.

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?

Dix answered 3/6, 2013 at 14:52 Comment(0)
B
6

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.

Brume answered 3/6, 2013 at 15:15 Comment(2)
one small question, though - if I am extending FragmentActivity does the above hold true? because onCreateView appears to be different for FragmentActivity than for Fragment and elsewhere on SO I was reading that onCreate is OK to use in FragmentActivityDix
Well an Activity doesn't have an onCreateView callback so you would have to use onCreate. I would recommend extending MapFragment and keep all your map stuff in the fragment.Brume

© 2022 - 2024 — McMap. All rights reserved.