方法一:
screenOrientation = application.getResources().getConfiguration().orientation;
方法二:
screenOrientation = activity.getRequestedOrientation();获取当前activity 的横竖方向
动态设置屏幕横竖屏:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
生命周期:
横竖屏切换
(1)无configChanges
onCreate-->
onStart-->
onResume-->
----------切换--------
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
-----------切换--------
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
(2)修改AndroidManifest.xml,把该Activity添加 android:configChanges="orientation",执行步骤3
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
再执行步骤4,发现不会再打印相同信息,但多打印了一行onConfigChanged
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onConfigurationChanged-->
(3)改成 android:configChanges="orientation|screenSize|keyboardHidden"(sdk>13时需加上screenSize),执行步骤3,就只打印onConfigChanged
onConfigurationChanged-->
执行步骤4
onConfigurationChanged-->
onConfigurationChanged-->
总结:
1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
2、设置Activity的android:configChanges="keyboardHidden"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
3、设置Activity的android:configChanges="orientation|screenSize|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法
原文地址:http://3387980.blog.51cto.com/3377980/1685333