Android Activity onConfigurationChanged()方法 监听状态改变

匿名 (未验证) 提交于 2019-12-03 00:26:01

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!