当前位置:操作系统 > 安卓/Android >>

横屏和竖屏切换不用销毁重新建立android:configChanges

 

在android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置。在activity加上android:configChanges="keyboardHidden|orientation"属性,就不会重启activity.而只是调用onConfigurationChanged(Configuration newConfig).这样就可以在这个方法里调整显示方式.

 

 

 

 

MainActivity中:

 

public void onConfigurationChanged(Configuration newConfig) { 

        try { 

            super.onConfigurationChanged(newConfig); 

            if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 

                Log.v("Himi", "onConfigurationChanged_ORIENTATION_LANDSCAPE"); 

            } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 

                Log.v("Himi", "onConfigurationChanged_ORIENTATION_PORTRAIT"); 

            } 

        } catch (Exception ex) { 

        } 

    } 

 

 

 

 

 

 

<?xml version="1.0" encoding="utf-8"?> 

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

 

    package="com.himi" android:versionCode="1" android:versionName="1.0"> 

 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

 

        <activity android:name=".MainActivity" android:label="@string/app_name" 

 

            android:configChanges="keyboardHidden|orientation"> 

 

            <intent-filter> 

 

                <action android:name="android.intent.action.MAIN" /> 

 

                <category android:name="android.intent.category.LAUNCHER" /> 

 

            </intent-filter> 

 

        </activity> 

 

    </application> 

 

    <uses-sdk android:minSdkVersion="4" /> 

 

</manifest>   

 

摘自 junjieking的专栏

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,