Background:
- Developing a native Android App that uses Android Google Map v2, uses
android.support.v4.app.FragmentActivity
. Runs on Android v2.2.
Objective:
- Retain Markers/Polylines "drawn" on map before the phone orientation is changed.
Question(s):
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 appropriatesavedInstance.put..
methods and then "restore" them inonCreate
using the appropriatesavedInstanceState.get..
methods.In reviewing the description for Marker
getID()
I'm confused when the Google documentation forMarker.getId()
method states the following: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.
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?
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 aput
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: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.
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?
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?
Regards.