Need to restore markers which are stored in HashMap<String, Marker> markers;
when fragment is opened back from another activity.
This is what i tried:
HashMap<String, Marker> markers;
//..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//..
this.markers = new HashMap<String, Marker>();
// Restoring the markers on configuration changes
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("markers")) {
markers = (HashMap<String, Marker>) savedInstanceState.getSerializable("markers");
if (markers != null) {
for (String key : markers.keySet()) {
Location location =
new Location(markers.get(key).getPosition().latitude, markers.get(key).getPosition().longitude);
addMarker(key, location);
}
}
}
}
return rootView;
}
public void addMarker(String key, Location location) {
//if (!key.equals(strUserID)) {
Marker marker = this.mGoogleMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.icon(BitmapDescriptorFactory.defaultMarker()));
}
//...
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("markers", markers);
}
I want to restore markers on fragment when fragment is resumed from the pause state from another activity. For Example: Activity A contains Fragment FA from which Activity B is called in foreground then on back-press again Fragment FA is opened:
Activity A (Fragment FA)(Markers shown on map and should store hashmap `onSaveInstanceState(Bundle outState)` when while activity B is called) ---> Activity B ---> (On Back-pressed restore markers from hashmap `if (savedInstanceState != null)`) Fragment FA.