AndroidManifest.xml文件:
1.增加权限android.permission.CHANGE_CONFIGURATION
2.设置Activity的android:configChanges属性

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /><application>        <activity            android:name=".MainActivity"            android:configChanges="orientation|screenSize"            android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>application>

android:screenOrientation="portrait" 禁止横竖屏切换
重写Activity中的onConfigurationChanged方法。

     * If any configuration change occurs that is not selected to be reported     * by that attribute, then instead of reporting it the system will stop     * and restart the activity (to have it launched with the new     * configuration).    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        Logger.d("onConfigurationChanged=====" + newConfig.toString());    }

如果设置android:configChanges="orientation|screenSize",手机屏幕旋转会执行onConfigurationChanged()方法,否则会重启当前activity。
参考https://blog.csdn.net/chencehnggq/article/details/23736869

更多相关文章

  1. ProgressBar的样式及用法
  2. wifi和GPRS下获取android的IP信息
  3. 安卓布局
  4. [长姿势了]android:padding和android:margin的区别
  5. android Java 笔试考题
  6. textview中有很多行,我只让它显示三行,最后显示...,我设置了androi
  7. Android主流三方库源码分析(五、深入理解RxJava源码)
  8. Android(安卓)sd卡操作的一些坑
  9. android:layout_gravity和android:gravity属性的区别

随机推荐

  1. GreenDao 3.0 简介、使用及踩坑
  2. Android(安卓)进阶解密读书笔记1
  3. Android(安卓)Studio 3.4 注解处理器不能
  4. Android(安卓)实现ListView 3D效果 - 1
  5. Unknown host ‘XXXX: nodename nor serv
  6. 一文详解Android(安卓)轻量级存储方案的
  7. Android(安卓)Secret Code
  8. Android的事件分发机制
  9. android 图形展示
  10. Android自定义Toast的时长、位置、及显示