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. Android高手进阶教程(二十)之---Android与JavaScript方法相互调
  2. android中的坐标系以及获取坐标的方法
  3. Android与JavaScript方法相互调用
  4. 创建Android库的方法及Android .aar文件用法小结
  5. Android各种资源引用的方法
  6. Android 性能优化的一些方法
  7. android 让一个控件按钮居于底部的几种方法

随机推荐

  1. android 使用Intent传递数据之返回结果
  2. android菜鸟学习笔记16----Android项目打
  3. 调用Android中的软键盘
  4. Android Activity总结
  5. android开发每日汇总【2011-11-10】
  6. 讨论会3:Android用户研究及4.0界面设计分
  7. Android Log系统介绍 (基于Android N)
  8. java/android 使用swig编译c/c++ 代码类
  9. Android学习之界面篇(二)Android(安卓)Anim
  10. linux下eclipse启动android模拟器出现PAN