I am trying to save the type of map that the user selected through the menu so the type of map remains even if the device will be rotated or the activity will be suspended for a few moments. This is what I did but it doesn't seem to work. Please can you tell me what am I doing wrong?
private int mapTypeSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null){
mapTypeSelected = GoogleMap.MAP_TYPE_NORMAL;
} else {
mapTypeSelected = savedInstanceState.getInt("the_map_type",GoogleMap.MAP_TYPE_NORMAL);
}
}
My main menu has more choices but I added here only the relevant ones for the map type:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.change_map_type:
Toast.makeText(getApplicationContext(), "Change map type", Toast.LENGTH_LONG).show();
return true;
case R.id.map_type_normal:
mapTypeSelected = GoogleMap.MAP_TYPE_NORMAL;
mMap.setMapType(mapTypeSelected);
return true;
case R.id.map_type_satellite:
mapTypeSelected = GoogleMap.MAP_TYPE_SATELLITE;
mMap.setMapType(mapTypeSelected);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then at the end of my activity I added this two:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("the_map_type", mapTypeSelected);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.get("the_map_type");
}
I am sure that is something stupid that I am doing wrong but I cannot seem to see what. There is no error and everything is working fine just that on rotation the map type changes always to normal. Thanks in advance for any help!