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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
lucas
  • 1,485
  • 13
  • 22

1 Answers1

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.

Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • 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 FragmentActivity – lucas Jun 03 '13 at 16:00
  • 1
    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. – Bobbake4 Jun 04 '13 at 13:56