android onSaveInstanceState()及其配对方法。

转自:http://blog.chinaunix.net/uid-22985736-id-2977672.html

onSaveInstanceState() 和 onRestoreInstanceState()不属于Activity的生命周期,只有意外销毁一个Activity时才被调用,如内存不足,按下了HOME键(注:按下BACK键则是主动销毁一个Activity,这两个方法不会被调用)。当需要改变屏幕方向时,也可以用这两个方法来暂存一些数据。

下面百度地图应用中的例子,就用到了这两种方法,用来保存和恢复地图的视图。
@Override
protected void onSaveInstanceState(Bundle outState) {
 cPoint = mapView.getMapCenter(); //得到当前MapView的中心点。
 outState.putInt("lat", cPoint.getLatitudeE6()); //暂存在outState中
 outState.putInt("lon", cPoint.getLongitudeE6());
 super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
 int lat = savedInstanceState.getInt("lat"); //从保存的数据中恢复
 int lon = savedInstanceState.getInt("lon");
 cPoint = new GeoPoint(lat, lon);
 super.onRestoreInstanceState(savedInstanceState);
}

原文地址:https://www.cnblogs.com/duanweishi/p/4507388.html