I've implmenented a simple Activity which contains two tabs. These tabs are associated with Fragments: the first one is a MapFragment
, the second is a ListFragment
.
I overrid the onCreate
method in this way:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.path_summary);
final ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab()
.setText("Map")
.setTabListener(new TabListener<com.google.android.gms.maps.MapFragment>(
this, "map", com.google.android.gms.maps.MapFragment.class)));
actionbar.addTab(actionbar.newTab()
.setText("List")
.setTabListener(new TabListener<com.example.northfinder.PathListFragment>(
this, "list", com.example.northfinder.PathListFragment.class)));
if (savedInstanceState != null) {
actionbar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
Then I need to write in the map (adding a marker), so I've override the onResume method:
protected void onResume() {
super.onResume();
FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentByTag("map");
if(f!= null && f.isAdded())
setUpMapIfNeeded((MapFragment)f);
}
where setUpIfNeeded
is defined below:
private static void setUpMapIfNeeded(MapFragment mMapFragment) {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = mMapFragment.getMap();
}
if (mMap != null) {
mMap.clear()
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
All works good. The marker is correctly added in the MapFragment when the Activity starts, and when I change tab and then I come back in the first tab the marker is yet visible.
I have just a problem: when orientation changes the marker disappears. Why?