APP横竖屏不切换,不重走生命周期
16lz
2022-05-19
手机切换横竖屏时,不重走生命周期
android:configChanges="orientation|keyboardHidden|screenSize"
设置手机一直保持竖屏状态,不重走生命周期
android:screenOrientation="portrait"
设置手机一直保持横屏状态,不重走生命周期
android:screenOrientation="landscape"
通过代码设置
部分手机,通过代码设置,不切换横竖屏,不重走生命周期
但是还有一部分手机,可能会切换1次横竖屏,重走生命周期,建议添加
android:configChanges="orientation|keyboardHidden|screenSize"
属性
/** * 判断是否平板设备 * @param context * @return true:平板,false:手机 */private boolean isTabletDevice(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;}//设置屏幕方向private void setScreenOrientation(Context context){ if(!isTabletDevice(context)){ //手机,竖屏 MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }else{ //平板,横屏 MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }}
推荐使用
两个一起设置android:screenOrientation="portrait"android:configChanges="orientation|keyboardHidden|screenSize"
借鉴
https://www.jianshu.com/p/dbc7e81aead2
更多相关文章
- Android系统手机端抓包方法
- xUtils,butterknife...处理findviewbyid
- Fragments的初识---android开发
- Eclipse Android(安卓)代码自动提示功能
- Android系列教程之六:TextView小组件的使用--附带超链接和跑马灯
- android程序在Layout中设置控件水平或垂直居中
- Android通过onDraw实现在View中绘图操作
- Android(安卓)访问权限设置记录-存档留着有用!
- Android(安卓)快速打包项目成jar包