特别注意onSaveInstanceState并不是生命周期方法
当设备旋转时,通过Log可以看出:
设备销毁Activity实例:
01-05 20:07:11.289 7860-7860/? D/QuizActivity: onPause(Bundle) called
01-05 20:07:11.290 7860-7860/? I/QuizActivity: onSaveInstanceState
01-05 20:07:11.290 7860-7860/? D/QuizActivity: onStop(Bundle) called
01-05 20:07:11.317 7860-7860/? D/QuizActivity: onDestroy(Bundle) called
重建Activity实例:
01-05 20:07:11.353 7860-7860/? D/QuizActivity: onCreate(Bundle) called
01-05 20:07:11.373 7860-7860/? D/QuizActivity: onStart(Bundle) called
01-05 20:07:11.377 7860-7860/? D/QuizActivity: onResume(Bundle)called
*继续深入
当我把setting->developer option->Don’t keep activities打开后再进行测试:
点击Back键
01-05 21:15:38.946 13590-13590/? D/QuizActivity: onPause(Bundle) called
01-05 21:15:39.878 13590-13590/? D/QuizActivity: onStop(Bundle) called
01-05 21:15:39.886 13590-13590/? D/QuizActivity: onDestroy(Bundle) called
发现onSaveInstanceState并没有被调用
点击Home键
01-05 21:17:43.975 13590-13590/? D/QuizActivity: onPause(Bundle) called
01-05 21:17:44.723 13590-13590/? I/QuizActivity: onSaveInstanceState
01-05 21:17:44.723 13590-13590/? D/QuizActivity: onStop(Bundle) called
01-05 21:17:44.741 13590-13590/? D/QuizActivity: onDestroy(Bundle) called
当把Don’t keep activities选项关闭后再进行上述相同测试发现:
唯一不同的是点击Home键依次执行:
01-05 21:17:43.975 13590-13590/? D/QuizActivity: onPause(Bundle) called
01-05 21:17:44.723 13590-13590/? I/QuizActivity: onSaveInstanceState
01-05 21:17:44.723 13590-13590/? D/QuizActivity: onStop(Bundle) called
Activity没有被Destroy
而点击Back键,Activity都会被销毁(也不会保存activity的状态,即:不调用onSaveInstanceState)。可以理解为Back键相当于通知系统“用户不再需要当前的Activity”。
内容参考:Android编程权威指南