Activity3---不经意的死掉onSaveInstanceState

android onSaveInstance方法

1周期



Activity3---不经意的死掉onSaveInstanceState_第1张图片
注:

2使用场景:

a来电话
b照相
c横竖屏
dhome键盘
e电源键
f新启动activity

3使用示例

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save the current data, for instance when changing screen orientation
outState.putSerializable("dataset", mDataset);
outState.putSerializable("renderer", mRenderer);
}
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
// restore the current data, for instance when changing the screen
// orientation
mDataset = (XYMultipleSeriesDataset) savedState.getSerializable("dataset");
mRenderer = (XYMultipleSeriesRenderer) savedState.getSerializable("renderer");
}


4配对对比

至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和 onRestoreInstanceState方法“不一定”是成对的被调用的,onRestoreInstanceState被调用的前提 是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity A的onRestoreInstanceState方法不会被执行。

你可能感兴趣的:(android,android)