android设置横竖屏切换时生命周期不会改变

来源:互联网 发布:淘宝阿迪达斯高仿店铺 编辑:程序博客网 时间:2024/05/23 15:52

为了不让activity在发生横竖屏切换时生命周期不要改变,

需要在配置文件中添加


android:configChanges="keyboardHidden|orientation|screenSize"
<activity android:name=".MainActivity"    android:configChanges="keyboardHidden|orientation|screenSize"/> 添加了这句代码后,在发生横竖屏切换时activity 的生命周期不会改变 下面的这个方法才会有效
@Overridepublic void onConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);    Configuration cfg = getResources().getConfiguration();    if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE) {        Toast.makeText(this, "横屏", Toast.LENGTH_SHORT).show();    } else if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {        Toast.makeText(this, "竖屏", Toast.LENGTH_SHORT).show();    }}



1 0
原创粉丝点击