我已使用Android Studio向导创建带有Google地图的APP。 在我的应用程序的主要活动有一个地图,我可以添加一些标记。此外,还有一个菜单可以调用其他3个活动。
当用户打开辅助活动然后关闭它(例如按下后退按钮或单击按钮以调用完成)时,辅助活动已关闭,但主活动始终是重新创建(onCreate方法是调用)和地图重新创建,所以我丢失了标记和相机选项。
我尝试使用onSaveInstanceState来保存标记和相机设置,但我无法恢复它,因为onCreate的savedInstanceState参数始终为null。 我也尝试使用onRestoreInstanceState,但是这个方法永远不会调用,除了我测试onSaveInstanceState,当我尝试打开辅助活动时,这总是调用。
我怎样才能避免每次都重新创建地图? savedInstanceState的问题在哪里?
这是我的应用程序代码(主要活动)的摘录:
public class MapsActivity extends FragmentActivity implements GoogleMap.OnInfoWindowClickListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener, GoogleMap.OnCameraChangeListener {
...
List<MapPoint> MapPointsList = new ArrayList<MapPoint>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
if (savedInstanceState != null) {
mCurrentMapCenterLatitude = savedInstanceState.getDouble("mCurrentMapCenterLatitude");
mCurrentMapCenterLongitude = savedInstanceState.getDouble("mCurrentMapCenterLongitude");
mCurrentMapZoom = savedInstanceState.getFloat("mCurrentMapZoom");
MapPointsList.clear();
MapPolylinesList.clear();
ArrayList<MapPointsParcelable> list;
list = savedInstanceState.getParcelableArrayList("MapPointsList");
for (MapPointsParcelable aList : list) {
MapPoint mapPoint = new MapPoint(aList.name, aList.latitude, aList.longitude, aList.status, aList.kind);
mapPoint.selected = aList.selected;
MapPointsList.add(mapPoint);
}
mapRestore = true;
}
}
@Override
protected void onResume() {
super.onResume();
setUpMap();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putDouble("mCurrentMapCenterLatitude", mCurrentMapCenterLatitude);
savedInstanceState.putDouble("mCurrentMapCenterLongitude", mCurrentMapCenterLongitude);
savedInstanceState.putFloat("mCurrentMapZoom", mCurrentMapZoom);
ArrayList<MapPointsParcelable> list;
list = new ArrayList<MapPointsParcelable>();
for (MapPoint aMapPointsList : MapPointsList) {
list.add(new MapPointsParcelable(aMapPointsList.name, aMapPointsList.latitude, aMapPointsList.longitude, aMapPointsList.status, aMapPointsList.kind, aMapPointsList.realname, aMapPointsList.selected));
}
savedInstanceState.putParcelableArrayList("MapPointsList", list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication()).inflate(R.menu.action_bar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearchActivity();
return true;
case R.id.action_settings:
openSettingsActivity();
return true;
case R.id.action_credits:
openCreditsActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
setUpMap();
}
}
if (mLocation == null) {
mLocation = ((LocationManager) getSystemService(LOCATION_SERVICE));
}
}
private void openSearchActivity() {
Intent intent = new Intent(this, SearchActivity.class);
startActivityForResult(intent, 1);
}
private void openSettingsActivity() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, 2);
}
private void openCreditsActivity() {
Intent intent = new Intent(this, CreditsActivity.class);
startActivity(intent);
}
...
}