【举个栗子】
我们继续使用 Activity : 生命周期 的代码,稍加改造一下。
1、在Activity1.Java追加2个函数
@Override protected void onSaveInstanceState(Bundle outState) { outState.putString("key1", "value1"); super.onSaveInstanceState(outState); Log.i("INFO", "Activity1: onSaveInstanceState, key1[value1]"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i("INFO", "Activity1: onRestoreInstanceState, key1[" + savedInstanceState.getString("key1") + "]"); }修改Activity1.Java的onCreate()函数中的log输出代码为
if(savedInstanceState != null) { Log.i("INFO", "Activity1: onCreate, key1[" + savedInstanceState.getString("key1") + "]"); } else { Log.i("INFO", "Activity1: onCreate"); } 2、在Activity2.Java追加2个函数 @Override protected void onSaveInstanceState(Bundle outState) { outState.putString("key1", "value1"); super.onSaveInstanceState(outState); Log.i("INFO", "Activity2: onSaveInstanceState, key1[value1]"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i("INFO", "Activity2: onRestoreInstanceState, key1[" + savedInstanceState.getString("key1") + "]"); } </pre><pre>修改Activity2.Java的onCreate()函数中的log输出代码为
if(savedInstanceState != null) { Log.i("INFO", "Activity2: onCreate, key1[" + savedInstanceState.getString("key1") + "]"); } else { Log.i("INFO", "Activity2: onCreate"); } 3、在Activity3.Java追加2个函数 @Override protected void onSaveInstanceState(Bundle outState) { outState.putString("key1", "value1"); super.onSaveInstanceState(outState); Log.i("INFO", "Activity3: onSaveInstanceState, key1[value1]"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i("INFO", "Activity3: onRestoreInstanceState, key1[" + savedInstanceState.getString("key1") + "]"); } 修改Activity2.Java的onCreate()函数中的log输出代码为 if(savedInstanceState != null) { Log.i("INFO", "Activity3: onCreate, key1[" + savedInstanceState.getString("key1") + "]"); } else { Log.i("INFO", "Activity3: onCreate"); }
然后启动程序,观察log输出。
1、跳转到ACTIVITY2时
Activity1: onPause Activity2: onCreate Activity2: onStart Activity2: onResume Activity1: onSaveInstanceState, key1[value1] Activity1: onStop 从ACTIVITY2返回ACTIVITY1时 Activity2: onPause Activity1: onCreate Activity1: onStart Activity1: onResume Activity2: onSaveInstanceState, key1[value1] Activity2: onStop2、跳转到ACTIVITY3时
Activity1: onPause Activity3: onCreate Activity3: onStart Activity3: onResume Activity1: onSaveInstanceState, key1[value1] 从ACTIVITY3返回ACTIVITY1时 Activity3: onPause Activity1: onResume Activity3: onStop Activity3: onDestroy 3、打开对话框 和 从对话框返回ACTIVITY1时(没有LOG输出) 4、按桌面按钮返回桌面时: Activity1: onPause Activity1: onSaveInstanceState, key1[value1] Activity1: onStop 从桌面再次打开APP时: Activity1: onRestart Activity1: onStart Activity1: onResume5、锁屏时:Activity1: onPause Activity1: onSaveInstanceState, key1[value1] Activity1: onStop解锁时:Activity1: onRestart Activity1: onStart Activity1: onResume6、横屏竖屏切换时(Ctrl + F12)Activity1: onPause Activity1: onSaveInstanceState, key1[value1] Activity1: onStop Activity1: onDestroy Activity1: onCreate, key1[value1] Activity1: onStart Activity1: onRestoreInstanceState, key1[value1] Activity1: onResume 从上面的log可以看出:发生1和2这种Activity间跳转时,被遮盖的Activity的onSaveInstanceState()函数被调用,Activity的临时数据被保存了起来。上面的log中没有读取被保存的数据,是因为系统没有发生内存不足的情况,被遮盖的Activity还没有被销毁。
返回桌面、锁屏两种情况也是保存了数据却没有读取数据,同理也是系统还没有销毁被遮盖的Activity。
横屏竖屏切换时,因为Activity肯定会被销毁然后重新创建,所以Activity的数据保存和读取都发生了,这种情况日常很常见,比如:
使用优酷等视频APP看视频时我们经常会在横屏和竖屏间切换(看视频:横屏,中途来了电话:竖屏,接完电话继续看视频:横屏),当切换横竖屏时,正在观看中的视频并没有从头播放,而是从中断处继续播放,这就是因为横竖屏切换时视频的播放进度被事先保存了起来,当切换完成后取出之前保存的进度,继续播放。