如何在3情況下保存MapView的狀態1)旋轉設備2)啓動新的碎片並返回上一個3)啓動新的碎片並旋轉設備。下面的代碼解析2事件:保存地圖狀態
public void onPause() {
Log.d(TAG, "onPause");
mMapView.onPause();
View view = mActivity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
super.onPause();
cameraPosition = mGoogleMap.getCameraPosition();
}
@Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume();
mMapView.onResume();
mMapView.getMapAsync(this);
}
這1個事件:
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
if(mSearchString !=null && !mSearchString.equals("")) {
outState.putString("search", mSearchString);
}
final Bundle mapViewSaveState = new Bundle();
mMapView.onSaveInstanceState(mapViewSaveState);
outState.putBoolean("firstStart", false);
outState.putBundle("mapState", mapViewSaveState);
super.onSaveInstanceState(outState);
}
但在3事件mMapView == null且NPE。怎麼解決?
是您在更改方向時重新啓動嗎? –
當然是的! –