android平板开发过程中出现 代码强制横屏(

android:screenOrientation="landscape"//AndroidManifest.xml中对activity设置
或者
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//代码中配置

)后,在息屏后重新打开,会先显示竖屏,然后再跳转到横屏,主要原因是在重新按亮屏幕时,activity的生命周期会重新走一遍,然后会出现竖屏下显示横屏的情况。


解决办法:

在AndroidManifest.xml中对activity设置android:configChanges="orientation|keyboardHidden",这样就解决了!


android:configChanges="xxx" 详解:

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

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

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


测试日志如下:

1.没有设置android:configChanges="orientation|keyboardHidden"

//息屏

11-22 19:40:05.126 31232-31232/com.example.xy.test D/TAG: onPause
11-22 19:40:05.128 31232-31232/com.example.xy.test D/TAG: onStop
11-22 19:40:05.687 31232-31232/com.example.xy.test D/TAG: onDestroy
11-22 19:40:05.814 31232-31232/com.example.xy.test D/TAG: onCreate
11-22 19:40:05.816 31232-31232/com.example.xy.test D/TAG: onResume
11-22 19:40:05.820 31232-31232/com.example.xy.test D/TAG: onPause
重新打开屏幕
11-22 19:40:17.283 31232-31232/com.example.xy.test D/TAG: onResume
11-22 19:40:17.425 31232-31232/com.example.xy.test D/TAG: onPause
11-22 19:40:17.425 31232-31232/com.example.xy.test D/TAG: onStop
11-22 19:40:17.425 31232-31232/scom.example.xy.test D/TAG: onDestroy
11-22 19:40:17.544 31232-31232/com.example.xyt.test D/TAG: onCreate
11-22 19:40:17.545 31232-31232/com.example.xy.test D/TAG: onResume

2.设置了android:configChanges="orientation|keyboardHidden"
//息屏
11-22 19:41:30.019 31969-31969/com.example.xy.test D/TAG: onPause
11-22 19:41:30.027 31969-31969/com.example.xy.test D/TAG: onStop
11-22 19:41:30.580 31969-31969/com.example.xy.test D/TAG: onConfigurationChanged
//重新打开屏幕
11-22 19:41:34.785 31969-31969/com.example.xy.test D/TAG: onRestart
11-22 19:41:34.831 31969-31969/com.example.xy.test D/TAG: onResume
11-22 19:41:34.904 31969-31969/com.example.xy.test D/TAG: onConfigurationChanged


更多相关文章

  1. Android的标题栏
  2. android clipChildren与clipToPadding
  3. Android(安卓)UI之对话框(dialog)
  4. Android(安卓)进程生命周期(Process Lifecycle)
  5. AMD windows7安装和打开genymotion、android studio遇到的问题
  6. Android(安卓)UI开发第七篇之Android(安卓)Gallery
  7. Android控件Editext、TextView属性详解
  8. android中LayoutParams设置参数的理解
  9. android 自定义dialog,窗口动画

随机推荐

  1. android MediaPlayer 架构介绍
  2. Android(安卓)Studio教程从入门到精通
  3. Android(安卓)源码下载
  4. Unity3D游戏引擎实现在Android中打开WebV
  5. ADK的安装
  6. Android(安卓)代码风格指南
  7. android屏幕适配详解
  8. Android(安卓)MediaPlayer
  9. MAC下Android的Eclipse开发环境的搭建
  10. Android(安卓)工具类 Toast Log.d()