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 进程生命周期(Process Lifecycle)
  2. Android Service生命周期
  3. Android屏幕截图实现 (adbd部分)
  4. Android屏幕信息获取
  5. Android 4.0: 旋转屏幕时禁止Activity重新加载
  6. android屏幕适配详解
  7. 必须知道的Android屏幕自适应解决方案
  8. Android中如何让手机屏幕不待机
  9. 最全面Android屏幕适配解决方案

随机推荐

  1. 作用域 常用函数 三元运算符及if条件
  2. Flink 数据持久化踩过的坑
  3. 用户表、商品表、订单表
  4. Redis实现分布式锁
  5. Java中的 9 个处理Exception的最佳实践,你
  6. 抽象类与接口、后期静态绑定与魔术方法简
  7. HTTP的恋爱史
  8. 23 个非常实用的 Shell 拿来就用脚本实例
  9. dblink不稳定造成 cursor: pin s wait on
  10. 新人和老人关于数据思维的50个区别