Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better configuration change handling

  • Loading branch information...
commit df319142e50c5a6186f8e9838531fe34455df692 1 parent 8af8df4
@commonsguy authored
View
53 MapsV2/Drag/src/com/commonsware/android/mapsv2/drag/MainActivity.java
@@ -35,6 +35,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener,
OnMarkerDragListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -52,27 +53,30 @@ protected void onCreate(Bundle savedInstanceState) {
SupportMapFragment mapFrag=
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
+ mapFrag.setRetainInstance(true);
initListNav();
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
-
- map.moveCamera(center);
- map.animateCamera(zoom);
-
- addMarker(map, 40.748963847316034, -73.96807193756104,
- R.string.un, R.string.united_nations);
- addMarker(map, 40.76866299974387, -73.98268461227417,
- R.string.lincoln_center,
- R.string.lincoln_center_snippet);
- addMarker(map, 40.765136435316755, -73.97989511489868,
- R.string.carnegie_hall, R.string.practice_x3);
- addMarker(map, 40.70686417491799, -74.01572942733765,
- R.string.downtown_club, R.string.heisman_trophy);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+
+ addMarker(map, 40.748963847316034, -73.96807193756104,
+ R.string.un, R.string.united_nations);
+ addMarker(map, 40.76866299974387, -73.98268461227417,
+ R.string.lincoln_center,
+ R.string.lincoln_center_snippet);
+ addMarker(map, 40.765136435316755, -73.97989511489868,
+ R.string.carnegie_hall, R.string.practice_x3);
+ addMarker(map, 40.70686417491799, -74.01572942733765,
+ R.string.downtown_club, R.string.heisman_trophy);
+ }
map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
map.setOnInfoWindowClickListener(this);
@@ -88,6 +92,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
+ @Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
View
49 MapsV2/Location/src/com/commonsware/android/mapsv2/location/MainActivity.java
@@ -38,6 +38,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener, LocationSource,
LocationListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -65,13 +66,15 @@ protected void onCreate(Bundle savedInstanceState) {
locMgr=(LocationManager)getSystemService(LOCATION_SERVICE);
crit.setAccuracy(Criteria.ACCURACY_FINE);
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
addMarker(map, 40.748963847316034, -73.96807193756104,
R.string.un, R.string.united_nations);
@@ -87,9 +90,24 @@ protected void onCreate(Bundle savedInstanceState) {
map.setOnInfoWindowClickListener(this);
map.setMyLocationEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(false);
- map.setLocationSource(this);
}
}
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ locMgr.requestLocationUpdates(0L, 0.0f, crit, this, null);
+ map.setLocationSource(this);
+ }
+
+ @Override
+ public void onPause() {
+ map.setLocationSource(null);
+ locMgr.removeUpdates(this);
+
+ super.onPause();
+ }
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
@@ -99,6 +117,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
+ @Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
@@ -106,12 +139,10 @@ public void onInfoWindowClick(Marker marker) {
@Override
public void activate(OnLocationChangedListener listener) {
this.mapLocationListener=listener;
- locMgr.requestLocationUpdates(0L, 0.0f, crit, this, null);
}
@Override
public void deactivate() {
- locMgr.removeUpdates(this);
this.mapLocationListener=null;
}
View
30 MapsV2/Markers/src/com/commonsware/android/mapsv2/markers/MainActivity.java
@@ -29,6 +29,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -50,13 +51,15 @@ protected void onCreate(Bundle savedInstanceState) {
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
addMarker(map, 40.748963847316034, -73.96807193756104,
R.string.un, R.string.united_nations);
@@ -77,6 +80,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return(true);
}
+ @Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
private void initListNav() {
ArrayList<String> items=new ArrayList<String>();
ArrayAdapter<String> nav=null;
View
30 MapsV2/NooYawk/src/com/commonsware/android/mapsv2/nooyawk/MainActivity.java
@@ -28,6 +28,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -49,13 +50,15 @@ protected void onCreate(Bundle savedInstanceState) {
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
}
}
@@ -66,6 +69,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return(true);
}
+ @Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
private void initListNav() {
ArrayList<String> items=new ArrayList<String>();
ArrayAdapter<String> nav=null;
View
30 MapsV2/Poly/src/com/commonsware/android/mapsv2/poly/MainActivity.java
@@ -35,6 +35,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -56,13 +57,15 @@ protected void onCreate(Bundle savedInstanceState) {
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
addMarker(map, 40.748963847316034, -73.96807193756104,
R.string.un, R.string.united_nations);
@@ -109,6 +112,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
+ @Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
View
30 MapsV2/Popups/src/com/commonsware/android/mapsv2/popups/MainActivity.java
@@ -32,6 +32,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -53,13 +54,15 @@ protected void onCreate(Bundle savedInstanceState) {
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
addMarker(map, 40.748963847316034, -73.96807193756104,
R.string.un, R.string.united_nations);
@@ -84,6 +87,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
+ @Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
View
30 MapsV2/Taps/src/com/commonsware/android/mapsv2/taps/MainActivity.java
@@ -35,6 +35,7 @@
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener,
OnMarkerClickListener {
+ private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
@@ -56,13 +57,15 @@ protected void onCreate(Bundle savedInstanceState) {
map=mapFrag.getMap();
- CameraUpdate center=
- CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
- -73.98180484771729));
- CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
+ if (savedInstanceState == null) {
+ CameraUpdate center=
+ CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
+ -73.98180484771729));
+ CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
- map.moveCamera(center);
- map.animateCamera(zoom);
+ map.moveCamera(center);
+ map.animateCamera(zoom);
+ }
addMarker(map, 40.748963847316034, -73.96807193756104,
R.string.un, R.string.united_nations, 45);
@@ -88,6 +91,21 @@ public boolean onNavigationItemSelected(int itemPosition, long itemId) {
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ super.onSaveInstanceState(savedInstanceState);
+
+ savedInstanceState.putInt(STATE_NAV,
+ getSupportActionBar().getSelectedNavigationIndex());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+
+ getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
+ }
+
+ @Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
Please sign in to comment.
Something went wrong with that request. Please try again.