横竖屏切换测试

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

@Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  Log.i(TAG,"onCreate-->");
 }
 
 @Override
 protected void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  Log.i(TAG,"onStart-->");
 }
 
 @Override
 protected void onRestart() {
  // TODO Auto-generated method stub
  super.onRestart();
  Log.i(TAG,"onRestart-->");
 }
 
 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  Log.i(TAG,"onResume-->");
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  Log.i(TAG,"onPause-->");
 }
 
 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  Log.i(TAG,"onStop-->");
 }
 
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Log.i(TAG,"onDestroy-->");
 }


 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onRestoreInstanceState(savedInstanceState);
  Log.i(TAG,"onRestoreInstanceState-->");
 }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);
  Log.i(TAG,"onSaveInstanceState-->");
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  // TODO Auto-generated method stub
  super.onConfigurationChanged(newConfig);
  Log.i(TAG,"onConfigurationChanged-->"+newConfig.orientation);
 }

 

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

10-23 02:35:54.261: INFO/chenys(4385): onCreate-->
10-23 02:35:54.271: INFO/chenys(4385): onStart-->
10-23 02:35:54.286: INFO/chenys(4385): onResume-->

 

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

10-23 02:36:58.331: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:36:58.411: INFO/chenys(4385): onPause-->
10-23 02:36:58.462: INFO/chenys(4385): onStop-->
10-23 02:36:58.481: INFO/chenys(4385): onDestroy-->
10-23 02:36:58.572: INFO/chenys(4385): onCreate-->
10-23 02:36:58.622: INFO/chenys(4385): onStart-->
10-23 02:36:58.632: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:36:58.642: INFO/chenys(4385): onResume-->

 

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

10-23 02:38:14.172: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:38:14.172: INFO/chenys(4385): onPause-->
10-23 02:38:14.172: INFO/chenys(4385): onStop-->
10-23 02:38:14.172: INFO/chenys(4385): onDestroy-->
10-23 02:38:14.281: INFO/chenys(4385): onCreate-->
10-23 02:38:14.301: INFO/chenys(4385): onStart-->
10-23 02:38:14.312: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:38:14.331: INFO/chenys(4385): onResume-->
10-23 02:38:14.812: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:38:14.852: INFO/chenys(4385): onPause-->
10-23 02:38:14.861: INFO/chenys(4385): onStop-->
10-23 02:38:14.892: INFO/chenys(4385): onDestroy-->
10-23 02:38:14.921: INFO/chenys(4385): onCreate-->
10-23 02:38:15.021: INFO/chenys(4385): onStart-->
10-23 02:38:15.031: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:38:15.111: INFO/chenys(4385): onResume-->

 

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

10-23 02:42:32.201: INFO/chenys(4875): onSaveInstanceState-->
10-23 02:42:32.232: INFO/chenys(4875): onPause-->
10-23 02:42:32.301: INFO/chenys(4875): onStop-->
10-23 02:42:32.311: INFO/chenys(4875): onDestroy-->
10-23 02:42:32.402: INFO/chenys(4875): onCreate-->
10-23 02:42:32.471: INFO/chenys(4875): onStart-->
10-23 02:42:32.471: INFO/chenys(4875): onRestoreInstanceState-->
10-23 02:42:32.481: INFO/chenys(4875): onResume-->

 

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

10-23 02:44:41.151: INFO/chenys(4875): onSaveInstanceState-->
10-23 02:44:41.151: INFO/chenys(4875): onPause-->
10-23 02:44:41.151: INFO/chenys(4875): onStop-->
10-23 02:44:41.151: INFO/chenys(4875): onDestroy-->
10-23 02:44:41.371: INFO/chenys(4875): onCreate-->
10-23 02:44:41.421: INFO/chenys(4875): onStart-->
10-23 02:44:41.521: INFO/chenys(4875): onRestoreInstanceState-->
10-23 02:44:41.541: INFO/chenys(4875): onResume-->
10-23 02:44:42.002: INFO/chenys(4875): onConfigurationChanged-->1

 

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

10-23 02:46:43.762: INFO/chenys(5193): onConfigurationChanged-->2

8、执行步骤4

10-23 02:47:27.652: INFO/chenys(5193): onConfigurationChanged-->2
10-23 02:47:27.902: INFO/chenys(5193): onConfigurationChanged-->1

 

 

 总结:

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次

2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次

3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

你可能感兴趣的:(android,测试)