文章目录
  1. 1. onSaveInstanceState
  2. 2. onRestoreInstanceState

onSaveInstanceState

当Activity可能会被销毁时,该Activity的onSaveInstanceState会被执行,除非Activity是主动销毁的。onSaveInstanceState的调用遵循一个规则,即未经允许,则onSaveInstanceState会被系统调用,这是一个保留数据的机会.
通常onSaveInstanceState被调用的情况有:

  1. 当用户按下HOME键时
  2. 长按HOME键,选择运行其他的程序时
  3. 按下电源键(关闭屏幕显示)时
  4. 从Activity启动新的Activity时
  5. 屏幕切换时
    屏幕切换时,系统会销毁Activity再自动创建Activity,onSaveInstance一定会被执行

onRestoreInstanceState

onRestoreInstanceState会在Activity被系统销毁后,被重建时调用,在Activity的onStart之后调用

1
2
3
4
5
6
7
8
9
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(icicle);
savedInstanceState.putLong("param", value);
}
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null){
value = savedInstanceState.getLong("param");
}
}

文章目录
  1. 1. onSaveInstanceState
  2. 2. onRestoreInstanceState