例2:横竖屏切换时候Activity的生命周期

  他切换时具体的生命周期是怎么样的:

  1、新建一个Activity,并把各个生命周期打印出来

  2、运行Activity,得到如下信息

  onCreate-->

  onStart-->

  onResume-->

  3、按crtl+f12切换成横屏时

  onSaveInstanceState-->

  onPause-->

  onStop-->

  onDestroy-->

  onCreate-->

  onStart-->

  onRestoreInstanceState-->

  onResume-->

  4、再按crtl+f12切换成竖屏时,发现打印了两次相同的log

  onSaveInstanceState-->

  onPause-->

  onStop-->

  onDestroy-->

  onCreate-->

  onStart-->

  onRestoreInstanceState-->

  onResume-->

  onSaveInstanceState-->

  onPause-->

  onStop-->

  onDestroy-->

  onCreate-->

  onStart-->

  onRestoreInstanceState-->

  onResume-->

  5、修改AndroidManifest.xml,把该Activity添加android:configChanges="orientation",执行步骤3

  onSaveInstanceState-->

  onPause-->

  onStop-->

  onDestroy-->

  onCreate-->

  onStart-->

  onRestoreInstanceState-->

  onResume-->

  6、再执行步骤4,发现不会再打印相同信息,但多打印了一行onConfigChanged

  onSaveInstanceState-->

  onPause-->

  onStop-->

  onDestroy-->

  onCreate-->

  onStart-->

  onRestoreInstanceState-->

  onResume-->

  onConfigurationChanged-->

  7、把步骤5的android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",执行步骤3,只打印onConfigChanged

  onConfigurationChanged-->