This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via {@link #onCreate} or {@link #onRestoreInstanceState}.
简单说就是:这个方法会在该activity被杀死之前调用,用来保存当前activity的一些状态.
经过测试,这个方法在onPause方法之后在onStop方法之前执行.
注意:当这个app所属的线程被杀死,也就是app重启之后,该方法不会执行,也就是该方法不能保存状态到app被杀死又重启之后.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (null!=savedInstanceState){
String onSaveInstanceState = savedInstanceState.getString("onSaveInstanceState", "");
System.out.println(onSaveInstanceState);
}
System.out.println("onCreate方法执行了");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("onSaveInstanceState","收到状态了");
System.out.println("执行了onSaveInstanceState方法");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart执行了");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause执行了");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume执行了");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("onRestart执行了");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop执行了");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy执行了");
}
}
当程序启动时,日志打印:
02-22 14:33:34.527 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onCreate方法执行了
02-22 14:33:34.537 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onStart执行了
02-22 14:33:34.537 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onResume执行了
然后横竖屏切换:
02-22 14:33:40.747 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onPause执行了
02-22 14:33:40.747 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: 执行了onSaveInstanceState方法
02-22 14:33:40.747 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onStop执行了
02-22 14:33:40.747 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onDestroy执行了
02-22 14:33:40.857 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: 收到状态了
02-22 14:33:40.857 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onCreate方法执行了
02-22 14:33:40.857 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onStart执行了
02-22 14:33:40.857 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onResume执行了
若果不进行横竖屏切换,只是锁屏:
02-22 14:38:14.697 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: onPause执行了
02-22 14:38:14.697 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: 执行了onSaveInstanceState方法
02-22 14:38:14.697 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: onStop执行了
再解锁:
02-22 14:40:14.157 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: onRestart执行了
02-22 14:40:14.157 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: onStart执行了
02-22 14:40:14.167 8065-8065/com.dgtech.onsaveinstancestatedemo I/System.out: onResume执行了
从上面我们可以看出,我们要注意,如果想收到上一次保存的activity状态,肯定是在onDestroy()方法执行之后,如果activity没有destroy也就没有必要保存状态
如果在正常状态杀掉进程呢?
02-22 14:37:58.077 2447-2447/com.dgtech.onsaveinstancestatedemo I/System.out: onPause执行了
进程被杀掉,只会执行到onPause方法,后面的方法不回执行,下次启动该activity就不能获得上一次activity的状态.